如何在列中获得最常见的值

时间:2013-11-28 14:09:28

标签: sql sql-server tsql

我在SQL server

中有这样的表
UsageTime        Website

10:45:08       yahoo.co.uk

10:24:06       msn.co.uk

09:45:08       lycos.co.uk

我需要得到像这样的东西

BusiestHour   NoOfWebsitesVisited
10:00         2     

我试过这个

SELECT TOP 3  UsageTime as BusiestHour, COUNT(Website) FROM  NoOfWebsitesVisited
GROUP BY BusiestHour    

但这不太对。它不检查整个小时,只检查特定值。

任何帮助将不胜感激:)谢谢

1 个答案:

答案 0 :(得分:5)

SELECT TOP 3   
       DATEPART(HOUR,UsageTime) as BusiestHour, 
       COUNT(Website) FROM  NoOfWebsitesVisited
GROUP BY DATEPART(HOUR,UsageTime)
ORDER BY NoOfWebsitesVisited DESC;

也许你想要这样的东西:

SELECT TOP 3 WITH TIES
       DATEPART(HOUR,UsageTime) as BusiestHour, 
       COUNT(Website) FROM  NoOfWebsitesVisited
GROUP BY DATEPART(HOUR,UsageTime)
ORDER BY NoOfWebsitesVisited DESC;