SQL Server 查询,最频繁的时间

发布于 2024-12-13 04:38:23 字数 784 浏览 0 评论 0原文

我有一个表,用于插入页面加载。有趣的字段是进入时间和国家/地区。

我如何查询,以便我可以按小时获取最频繁的国家/地区。结果集应如下所示(时间,国家/地区):

  • 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对不⑦ 2024-12-20 04:38:23

你快到了!

SELECT DatePart(hour, entrytime) AS h, country
FROM rpageload 
GROUP BY DatePart(hour, entrytime), country
ORDER BY COUNT(*) DESC

但是,这将合并每天每小时的条目
因此,昨天下午 1-2 点之间将与今天下午 1-2 点一起计算。

如果不希望出现这种情况,则需要在 WHERE 子句中过滤掉其他日期。
或者,在小时之上按天分组

编辑:
事实上,我没有正确阅读问题。

SELECT DatePart(hour, entrytime) AS h, country
FROM rpageload rp1
GROUP BY DatePart(hour, entrytime), country
HAVING COUNT(*) = 
 (SELECT MAX(COUNT(*))
  FROM rpageload rp2
  WHERE DatePart(rp2.hour, rp2.entrytime) = DatePart(rp1.hour, rp1.entrytime)
  GROUP BY DatePart(hour, entrytime), country
 )
ORDER BY h

当两个或多个国家/地区的计数相同且为该小时的最大值时,会发生什么情况?

或者,

WITH RPL1 AS
(
  SELECT DatePart(hour, entrytime) AS h, country, COUNT(*) AS cnt
  FROM rpageload rp1
  GROUP BY DatePart(hour, entrytime), country
),
RPL2 AS
(
  SELECT h, MAX(cnt) as maxcnt
  FROM RPL1
  GROUP BY h
)
SELECT RPL1.h, country
FROM RPL1
JOIN RPL2 ON RPL1.h = RPL2.h AND RPL1.cnt = RPL2.maxcnt

最后两个查询将返回与相同最大频率匹配的所有行。因此,同一小时可能会在结果集中出现多次。

如果您想过滤掉这些内容,请查看 ROW_NUMBER

WITH RPL1 AS
(
  SELECT DatePart(hour, entrytime) AS h, country, COUNT(*) AS cnt
  FROM rpageload rp1
  GROUP BY DatePart(hour, entrytime), country
),
RPL2 AS
(
  SELECT h, MAX(cnt) as maxcnt
  FROM RPL1
  GROUP BY h
),
DUPES AS
(
  SELECT RPL1.h, country, cnt, ROW_NUMBER() OVER(PARTITION BY RPL1.h ORDER BY country) AS rn
  FROM RPL1
  JOIN RPL2 ON RPL1.h = RPL2.h AND RPL1.cnt = RPL2.maxcnt
)
SELECT h, country, cnt
FROM DUPES
WHERE rn = 1

You're almost there!

SELECT DatePart(hour, entrytime) AS h, country
FROM rpageload 
GROUP BY DatePart(hour, entrytime), country
ORDER BY COUNT(*) DESC

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.

SELECT DatePart(hour, entrytime) AS h, country
FROM rpageload rp1
GROUP BY DatePart(hour, entrytime), country
HAVING COUNT(*) = 
 (SELECT MAX(COUNT(*))
  FROM rpageload rp2
  WHERE DatePart(rp2.hour, rp2.entrytime) = DatePart(rp1.hour, rp1.entrytime)
  GROUP BY DatePart(hour, entrytime), country
 )
ORDER BY h

What should happen when two or more countries have the same count and it's the maximum for that hour?

Alternatively,

WITH RPL1 AS
(
  SELECT DatePart(hour, entrytime) AS h, country, COUNT(*) AS cnt
  FROM rpageload rp1
  GROUP BY DatePart(hour, entrytime), country
),
RPL2 AS
(
  SELECT h, MAX(cnt) as maxcnt
  FROM RPL1
  GROUP BY h
)
SELECT RPL1.h, country
FROM RPL1
JOIN RPL2 ON RPL1.h = RPL2.h AND RPL1.cnt = RPL2.maxcnt

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

WITH RPL1 AS
(
  SELECT DatePart(hour, entrytime) AS h, country, COUNT(*) AS cnt
  FROM rpageload rp1
  GROUP BY DatePart(hour, entrytime), country
),
RPL2 AS
(
  SELECT h, MAX(cnt) as maxcnt
  FROM RPL1
  GROUP BY h
),
DUPES AS
(
  SELECT RPL1.h, country, cnt, ROW_NUMBER() OVER(PARTITION BY RPL1.h ORDER BY country) AS rn
  FROM RPL1
  JOIN RPL2 ON RPL1.h = RPL2.h AND RPL1.cnt = RPL2.maxcnt
)
SELECT h, country, cnt
FROM DUPES
WHERE rn = 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文