多个分组,总和

时间:2015-03-19 21:19:26

标签: sql

我的表:

ID | TIME OF CREATION | OWNER | STATE
1    2015-1-1           arpan   A  
2    2015-1-2           arpan   B 

我的查询中我想要的o / p是:

DATE | OWNER | COUNT(STATE = A) | COUNT(STATE = B) | ...

我检查了SUM( CASE ),但您不能按日期分组并按所有者排序?

坚持到这里。 :(

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

我认为你只想要条件聚合:

select date, owner, sum(case when state = 'A' then 1 else 0 end) as state_A,
       sum(case when state = 'B' then 1 else 0 end) as state_b
from table t
group by date, owner;