decode與case when的區別
求和:sum(decode(sign(xingji - 6),'',h.real_count,1,h.real_count,0))
decode相較於case when,函數裡面不能使用and連接多個條件,只能使用函數嵌套
例如:sum(decode(sign(xingji - 6),-1,decode(hh.quxian,18,h.real_count,19,h.real_count,20,h.real_count,24,h.real_count,
34,h.real_count,35,h.real_count,0),0))
www.2cto.com
求和:sum(case when start_time = '24' then quantity else 0 end)
case when顯得有些臃腫,但是在多個條件連接時比較方便,例如:sum(case when
start_time = '24' and city_code = '010' then quantity else 0 end)
另說明:sum()函數用於求和,sign()函數用於返回參數是大於0,還是小於0或者等於0,
一個例外是返回空:'';
例如:sign(1)--->1,sign(-1)--->-1,sign(0)--->0,sign(null)--->'';