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))

求和: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)--->'';

猜你喜欢

转载自lxzqz.iteye.com/blog/1738906
今日推荐