我有以下查询
Select count(*) as TotalCount from ABC where ID in (1,3,6)
这里的问题是它给了我1,3和6的总数。但我想得到每个ID的计数,如下所示:
TotalCount, ID
6, 1
2, 3
5, 6
请告诉我实现此目的的SQL Server查询。我不想使用临时表。
答案 0 :(得分:3)
只需将ID添加到所选列并按其分组:
Select id, count(*) as TotalCount from ABC where ID in (1,3,6)
group by id
答案 1 :(得分:0)
select count(*),id from abc where id in (1,3,6) group by id;