简体   繁体   English

SQL用于计算每小时的记录数,并计算总记录数大于一个值的小时数

[英]SQL to count records per hour and count how many hours that total record count was greater than a value

Using SQL Server 2014. I have a list of records that are time stamped and I would like to count how many records there are per hour then count how many hours each day that the record count exceeded a given number, say 20. 使用SQL Server 2014.我有一个带有时间戳的记录列表,我想计算每小时有多少记录,然后计算每天记录计数超过给定数量的小时数,比如20。

Here's what I have so far: 这是我到目前为止所拥有的:

select count(distinct datepart(hour, convert(datetime, OrderStateDate))) Count_HoursOver,
    datepart(YEAR, convert(datetime, OrderStateDate)) Date_YEAR,
    datepart(month, convert(datetime, OrderStateDate)) Date_MONTH,
    datepart(day, convert(datetime, OrderStateDate)) Date_DAY
from Reporting.dbo.Orders
group by datepart(YEAR, convert(datetime, OrderStateDate)),
    datepart(month, convert(datetime, OrderStateDate)),
    datepart(day, convert(datetime, OrderStateDate))
having count(idscript) >= 20

The results aren't correct and I can't make sense of what's being returned and why. 结果不正确,我无法理解返回的内容和原因。 Am I using HAVING incorrectly? 我不正确地使用HAVING吗? Any advice would be appreciated! 任何意见,将不胜感激!

You kind of have a 2-part question here 你有两个问题在这里

I would like to count how many records there are per hour 我想算一下每小时有多少条记录

You can create a query that returns tuples (RecordsPerHour,HOUR,YEAR,MONTH,DAY) as follows: 您可以创建一个返回元组(RecordsPerHour,HOUR,YEAR,MONTH,DAY)的查询,如下所示:

SELECT 
    count(*) as RecordsPerHour, 
    datepart(hour,convert(datetime,OrderStateDate)) as Date_HOUR, 
    datepart(year,convert(datetime,OrderStateDate)) as Date_YEAR,
    datepart(month,convert(datetime,OrderStateDate)) as Date_MONTH,
    datepart(day,convert(datetime,OrderStateDate)) as Date_DAY
FROM Reporting.dbo.Orders
GROUP BY
    datepart(year,convert(datetime,OrderStateDate)),
    datepart(month,convert(datetime,OrderStateDate)),
    datepart(day,convert(datetime,OrderStateDate)),
    datepart(hour,convert(datetime,OrderStateDate))

then count how many hours each day that the record count exceeded a given number, say 20 然后计算每天记录计数超过给定数量的小时数,比如20

To do this, use the query from the first part of your question in a nested query, using a HAVING clause to filter only hours that contain at least 20 orders. 为此,请在嵌套查询中使用问题第一部分中的查询,使用HAVING子句仅过滤包含至少20个订单的小时数。

On the outer query, group by (YEAR,MONTH,DAY) to determine the number of hours in that day with at least 20 orders: 在外部查询上,按(年,月,日)分组以确定当天至少有20个订单的小时数:

SELECT
    count(*) as HoursWithAtLeast20Orders, 
    Date_YEAR,
    Date_MONTH,
    Date_DAY
FROM
    (SELECT 
        datepart(hour,convert(datetime,OrderStateDate)) as Date_HOUR, 
        datepart(year,convert(datetime,OrderStateDate)) as Date_YEAR,
        datepart(month,convert(datetime,OrderStateDate)) as Date_MONTH,
        datepart(day,convert(datetime,OrderStateDate)) as Date_DAY
    FROM Reporting.dbo.Orders
    GROUP BY
        datepart(year,convert(datetime,OrderStateDate)),
        datepart(month,convert(datetime,OrderStateDate)),
        datepart(day,convert(datetime,OrderStateDate)),
        datepart(hour,convert(datetime,OrderStateDate))
    HAVING count(*) >=20) as t
GROUP BY
    Date_YEAR,
    Date_MONTH,
    Date_DAY

First round to hour then round to day 第一轮到第一轮,然后是第一轮

SELECT 
   count(*) as [hours with 20+ Orders],
   dateadd(day, datediff(day,'20000101',dt_hour_rounded),'20000101') as dt_day_rounded     
FROM (
   SELECT
      count(*) as OrdersInHour,
      dateadd(hour, datediff(hour,'20000101',OrderStateDate),'20000101') as dt_hour_rounded
   FROM Reporting.dbo.Orders
   GROUP BY dateadd(hour, datediff(hour,'20000101',OrderStateDate),'20000101')
) t
GROUP BY dateadd(day,datediff(day,'20000101',dt_hour_rounded),'20000101')
WHERE OrdersInHour >= 20

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM