检索分组项目的计数

时间:2015-03-15 22:37:04

标签: sql sql-server

我想根据分组的项目获取查询的某些结果的计数。我有这个问题:

SELECT c.name FROM tblCareers AS cc
INNER JOIN tblClusters AS c
ON c.careerid = cc.careerid
INNER JOIN @temp AS t
ON cc.userid = CurrentID

这给了我结果:

Agriculture
Healthcare
Agriculture

我希望能够返回的是:

Agriculture 2
Healthcare 1

所以我需要某种Count(c.name),但只返回:

Agriculture 1
Healthcare 1
Agriculture 1

有任何建议或如何?

谢谢!

1 个答案:

答案 0 :(得分:0)

您必须添加group by子句:

SELECT c.name, Count(*) FROM tblCareers AS cc
LEFTJOIN tblClusters AS c
ON c.careerid = cc.careerid
LEFT JOIN @temp AS t
ON cc.userid = CurrentID
Group by c.name
order by Count(*) DESC