我有谁使用在学院开设的课程/铂族金属学生的表。我想知道,在一个给定的一个月,前100铂族金属:
我得到这个MS SQL 2010下面的代码......找到前100铂族金属/节目。我真的清理我的黑客周围嵌套的SELECT中,COUNT,COUNT DISTINCT,RANK,等等......相信我,我已经尝试过,其他的问题及答案复制SQL。我希望不要有TP做一个临时表。一切我想是“不快乐” :(所以我想我会通过电子邮件发送的StackOverflow社区。任何帮助将不胜感激,谢谢你提前。
SELECT TOP 100
T.PGM AS 'Program'
,COUNT (*) AS 'Program Count'
FROM StudentPrograms T
WHERE
T.PGM <>'unknown'
AND T.CreateDate > '10/01/2015'
AND T.CreateDate < '10/31/2015'
GROUP BY
T.PGM
ORDER BY
COUNT(*) DESC
;
我应该结束了在表5列
如果你想要的排名,或许会rank()
确实想你想:
SELECT TOP 100
T.PGM AS Program, COUNT(*) AS ProgramCount,
RANK() OVER (Order BY COUNT(*) DESC) as ranking
FROM StudentPrograms T
WHERE T.PGM <>'unknown' AND
T.CreateDate > '2015-10-01' AND
T.CreateDate < '2015-10-31' -- We don't like Halloween
GROUP BY T.PGM
ORDER BY COUNT(*) DESC;