我的表中有一个字段flag,flag='加'和flag='回'。其中flag='加'有3条记录,flag=‘回’有2条记录。我想用一个查询语句一次性把,加、回各有几条记录都查询出来,请问该怎么做?谢谢!
------解决方案--------------------
------解决方案--------------------
select sum(case when flag='加' then 1 else 0 end) [加],sum(case when flag='回' then 1 else 0 end) [回] from tb
select flag , count(1) cnt from tb group by flag
------解决方案--------------------
select sum(case when flag = '加' then 1 else 0 end) as Jflag,
sum(case when flag = '回' then 1 else 0 end) as Hflag
from T
group by flag
------解决方案--------------------
select sum(case when flag = '加' then 1 else 0 end) as Jflag,
sum(case when flag = '回' then 1 else 0 end) as Hflag
from T
或
select flag,count(*) as FlagNum
from T
group by flag