SQL Server 查询,最频繁的时间
我有一个表,用于插入页面加载。有趣的字段是进入时间和国家/地区。
我如何查询,以便我可以按小时获取最频繁的国家/地区。结果集应如下所示(时间,国家/地区):
- 01 - 美国
- 02 - 新加坡
- 03 - 印度
- 04 - 丹麦
... 等等。
我开始做类似的事情
select DatePart(hour, entrytime) AS h, country from rpageload group by DatePart(hour, entrytime), country
,但我认为我走错了方向。 ORDER BY 和 COUNT(*) 需要适合某个地方:)
- 编辑/添加 -
我发现这种方式给了我正确的结果。但显然我也得到了很多结果集,而不仅仅是一个,就像我想要的那样。
DECLARE @count INT
SET @count = 0
WHILE (@count < 24)
BEGIN
SELECT TOP 1 @count AS hr, COUNT(*) AS nbr, country FROM rpageload WHERE DATEPART(hour, entrytime) = @count GROUP BY country ORDER BY nbr DESC
SET @count = (@count + 1)
END
只是为了澄清:日期或日期并不重要。目的是显示一天中不同时间哪个国家最常出现
I have a table where I insert page loads. The interesting fields are entrytime and country.
How do I query, so that I can get the most frequent country by hour. The result set should look like this (Hour, Country):
- 01 - USA
- 02 - Singapore
- 03 - India
- 04 - Denmark
... and so on.
I was getting started with something like
select DatePart(hour, entrytime) AS h, country from rpageload group by DatePart(hour, entrytime), country
but I think I am going in the wrong direction. ORDER BY and COUNT(*) needs to fit in somewhere :)
-- EDITED / ADDED --
I have found out that this sort-of gives me the right results. But I obviously also get many result sets, and not just one, like I'd like.
DECLARE @count INT
SET @count = 0
WHILE (@count < 24)
BEGIN
SELECT TOP 1 @count AS hr, COUNT(*) AS nbr, country FROM rpageload WHERE DATEPART(hour, entrytime) = @count GROUP BY country ORDER BY nbr DESC
SET @count = (@count + 1)
END
Just to clarify: The day or date doesn't matter. The purpose is to show which country is most frequent in the different hours of the day
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你快到了!
但是,这将合并每天每小时的条目
因此,昨天下午 1-2 点之间将与今天下午 1-2 点一起计算。
如果不希望出现这种情况,则需要在 WHERE 子句中过滤掉其他日期。
或者,在小时之上按天分组
编辑:
事实上,我没有正确阅读问题。
当两个或多个国家/地区的计数相同且为该小时的最大值时,会发生什么情况?
或者,
最后两个查询将返回与相同最大频率匹配的所有行。因此,同一小时可能会在结果集中出现多次。
如果您想过滤掉这些内容,请查看 ROW_NUMBER
You're almost there!
However, this will combine entries from every single day per hour
So between 1-2pm yesterday will be counted together with 1-2pm today
If this is undesired, you need to filter out other days in a WHERE clause.
Alternatively, group by days on top of the hours
Edit:
Actually, I didn't read the question properly.
What should happen when two or more countries have the same count and it's the maximum for that hour?
Alternatively,
The last two queries will return all rows matching the same maximum frequency. SO, the same hour may appear more than once in the resultset.
If you want to filter those out, look at ROW_NUMBER