我发现了类似于我想要的东西here,但它并没有给出正确的输出,而且我正试图想出正确的解决方案......
我们有一个AVS服务器(实时帐户验证服务,但这是无关紧要的),我想知道今天(或任何其他日子)每小时有多少请求。例如,假设在上午8点到9点之间发出了100个请求,并且在9点到10点之间发出了50个请求。输出在 8AM 时应 100 , 9AM 时 150 ,但我的查询显示的是请改为每小时一次。
我可以看到它为什么这样做......它是分组 小时所以它向我显示每小时的请求,但这不是我想要的,这是每小时的总运行。 (所以我每天都可以运行它,看看昨天这个请求的数量是什么样的等等......并且可以看出这是一个缓慢或快速的一天。)我该怎么做?
DECLARE @CurrentDate DATETIME = '14 nov 2014'
SELECT
CAST(DateCreated AS DATE) [Date],
DATEPART(hour,DateCreated) [Hour],
COUNT(ID) [Requests]
FROM Request
WHERE
DateCreated >= @CurrentDate AND Request.DateCreated < DATEADD(DAY, 1, @CurrentDate)
GROUP BY
CAST(DateCreated AS DATE),
DATEPART(hour, DateCreated)
ORDER BY
CAST(DateCreated AS DATE)
答案 0 :(得分:2)
以下内容应该有效
DECLARE @CurrentDate DATETIME = '13 nov 2014'
SELECT DISTINCT
CAST(DateCreated AS DATE) [Date],
DATEPART(hour,DateCreated) [Hour],
COUNT(ID) OVER (ORDER BY DATEPART(hour, DateCreated))
FROM Request
WHERE
DateCreated >= @CurrentDate AND DateCreated < DATEADD(DAY, 1, @CurrentDate)
ORDER BY
DATEPART(hour, DateCreated)
答案 1 :(得分:1)
感谢您的所有答案。他们中没有一个完全符合我的要求,所以这是我的最终查询,以防它可以帮助某人。答案的问题,也可能是我的问题,结果只显示每小时的总请求数,而不是当天的最终总数。我的下面的查询给出了每小时的请求,以及当天的最终请求,或当前总数(如果是当天)。
DECLARE @CurrentDate DATETIME = '17 Nov 2014'
SELECT * FROM (
SELECT DISTINCT
CAST(DateCreated AS DATE) [Date],
CASE
WHEN LEN(CAST(DATEPART(hour,DateCreated) AS VARCHAR)) = 1 THEN '0' + CAST(DATEPART(hour,DateCreated) AS VARCHAR)
ELSE CAST(DATEPART(hour,DateCreated) AS VARCHAR)
END + ':00:00' [Time],
rank() OVER (ORDER BY DATEPART(hour, DateCreated))[Requests]
FROM Request
WHERE
DateCreated >= @CurrentDate AND DateCreated < DATEADD(DAY, 1, @CurrentDate)
UNION
SELECT
CAST(@CurrentDate AS DATE),
CONVERT(VARCHAR(8),
(SELECT TOP 1 DateCreated FROM Request WHERE DateCreated >= @CurrentDate AND Request.DateCreated < DATEADD(DAY, 1, @CurrentDate) ORDER BY DateCreated DESC),
108),
(SELECT COUNT(*) from Request WHERE DateCreated >= @CurrentDate AND Request.DateCreated < DATEADD(DAY, 1, @CurrentDate))
) t
WHERE t.Requests > 0
ORDER BY t.Time
今天运行查询会产生如下输出:
+------------+----------+----------+
| Date | Time | Requests |
+------------+----------+----------+
| 2014-11-17 | 07:00:00 | 1 |
| 2014-11-17 | 08:00:00 | 9 |
| 2014-11-17 | 09:00:00 | 49 |
| 2014-11-17 | 10:00:00 | 113 |
| 2014-11-17 | 11:00:00 | 225 |
| 2014-11-17 | 12:00:00 | 294 |
| 2014-11-17 | 13:00:00 | 363 |
| 2014-11-17 | 13:09:46 | 383 |
+------------+----------+----------+
答案 2 :(得分:0)
DECLARE @CurrentDate DATETIME = '14 nov 2014';
WITH hourValues(hourVal) as (
select distinct cast(convert(varchar(14), @CurrentDate, 120) + '00' as datetime)
from [Request]
where datediff(day, [DateCreated], @CurrentDate) = 0)
SELECT
CAST(DateCreated AS DATE) [Date],
hourVal,
COUNT(case when [DateCreated] < dateadd(hour, 1, hourVal) then 1 else null end) [Requests]
FROM Request, hourValues
WHERE
datediff(day, [DateCreated], @CurrentDate) = 0
GROUP BY
CAST(DateCreated AS DATE),
hourVal
ORDER BY
[Date],
HourVal
答案 3 :(得分:0)
--if you use MS SQL Server 2008 R2 or later version (2012/2014) then try this
DECLARE @CurrentDate DATE = CONVERT(DATE, GETDATE() - 1) , --yesterday
@FinishDate AS DATE = CONVERT(DATE, GETDATE() + 1) --tomorrow
SELECT DISTINCT
CONVERT(DATE, DateCreated) AS [Date] ,
DATEPART(HOUR, DateCreated) AS [Hour] ,
COUNT(ID) OVER ( PARTITION BY CONVERT(DATE, DateCreated) ORDER BY DATEPART(HOUR,
DateCreated) ) AS [Cumulative Count]
FROM Request
WHERE CONVERT(DATE, DateCreated) BETWEEN @CurrentDate
AND @FinishDate
ORDER BY CONVERT(DATE, DateCreated) ,
DATEPART(HOUR, DateCreated)