id date val
12006 2005-3-5 10680.00
12046 2005-3-5 2840.00
12013 2005-3-5 12420.00
12010 2005-3-5 44640.00
12002 2005-3-5 81390.00
12005 2005-3-5 24943.00
12011 2005-3-5 10236.00
12030 2005-3-5
12003 2005-3-5 176830.00
12009 2005-3-5 158248.00
12014 2005-3-5 12500.00
12011 2005-3-6 11257.00
12012 2005-3-6 0.00
需求:按照每一天统计总和,但是不包括,12014,13003,12030,当id=12013时候,取负值
sql:写法一
select t.date,sum(
case
when id='12014' then 0
when id='13003' then 0
when stcdt='12030' then 0
when stcdt='12013' then -val
else val end)
from tb t group by t.date order by t.date desc;
写法二:
select t.date,sum(decode(t.id,'12014',0,'13003',0,'12030',0,'12013',-val,val))
from tb t group by t.date order by t.date desc;
写法三:
select date,sum( case when id='12013' then -val else val end) from tb t where id not in ('12014','13003','12030') group by t.date order by t.date desc;