我的数据库中有一个表(SQL Server):
表1:
white-space: wrap;
我想要这个结果
ID Name
1 a
2 a
3 b
我该怎样处理它?</ p>
答案 0 :(得分:3)
将汇总功能 COUNT
与 GROUP BY
条款一起使用。
<强>查询强>
select [Name], count([ID]) as [Count]
from [your_table_name]
group by [Name]
order by [Name];
或者,如果您想按count
的降序对结果进行排序,那么
select [Name], count([ID]) as [Count]
from [your_table_name]
group by [Name]
order by count([ID]) desc; -- for ascending order remove desc