求一个SQL的方法

来源:百度知道 编辑:UC知道 时间:2024/05/27 18:34:48
我有如下情况:
查询 user表中 按info字段分组结果,并且还差查询这个结果中 chk=1的有多少,即结果大约为:
info count chk=1
xxx 10 3
aaa 20 15
请问如何写SQL,十分感谢

select info,
count(info) [count],
sum(case chk when 1 then 1 else 0 end) [chk=1]
from [user]
group by info

漏加了中括号,现在OK了

select info,count(info) count,count(chk) chk=1
from user
group by info,chk
having chk=1

这个只能查出chk=1的数据,如果某个info没有chk=1的数据不会显示的。

select info,count(info) as [count],count(chk) as[chk=1]
from [user]
group by info,chk
having chk=1

select info,count(info),count(chk) as chk=1 from user
where chk=1
group by info
order by info