我遇到查询问题,必须计算行数并返回数字。关键是我需要最近24小时的数据,除以一小时的时间。我已经24次使用UNION ALL完成了这个查询,但是有超过800行的SQL单一查询(令人惊讶的是它只需要3秒)。我知道我可以按时间分组,但不知道如何正确地做到这一点。我坚信同等查询可能只在大约20-30行SQL中。我会感谢任何线索。在这里,您可以对我提到的那些进行简化查询。
长查询(使用UNION ALL):
DECLARE @CurrentTime datetime = GETDATE();
--Data from 1 hour
SELECT
----Time
(SELECT CONVERT(VARCHAR(5), (DATEADD(HOUR, -1, @CurrentTime)), 114)) AS [Time],
----Count on 1st table
(SELECT COUNT(T1.[TableFirstId])
FROM [dbo].[Table1] AS T1
WHERE T1.[IncomingDate] BETWEEN (DATEADD(HOUR, -1, @CurrentTime)) AND @CurrentTime) AS [CountT1],
----Count on 2nd table
(SELECT COUNT(T2.[TableSecondId])
FROM [dbo].[Table2] AS T2
WHERE T2.[IncomingDate] BETWEEN (DATEADD(HOUR, -1, @CurrentTime)) AND @CurrentTime) AS [CountT2],
----Count on 3rd table
(SELECT COUNT(T3.[TableThirdId])
FROM [dbo].[Table3] AS T3
WHERE T3.[IncomingDate] BETWEEN (DATEADD(HOUR, -1, @CurrentTime)) AND @CurrentTime) AS [CountT3]
UNION ALL
--Data from 2 hours
SELECT
----Time
(SELECT CONVERT(VARCHAR(5), (DATEADD(HOUR, -2, @CurrentTime)), 114)) AS [Time],
----Count on 1st table
(SELECT COUNT(T1.[TableFirstId])
FROM [dbo].[Table1] AS T1
WHERE T1.[IncomingDate] BETWEEN (DATEADD(HOUR, -2, @CurrentTime)) AND (DATEADD(HOUR, -1, @CurrentTime))) AS [CountT1],
----Count on 2nd table
(SELECT COUNT(T2.[TableSecondId])
FROM [dbo].[Table2] AS T2
WHERE T2.[IncomingDate] BETWEEN (DATEADD(HOUR, -2, @CurrentTime)) AND (DATEADD(HOUR, -1, @CurrentTime))) AS [CountT2],
----Count on 3rd table
(SELECT COUNT(T3.[TableThirdId])
FROM [dbo].[Table3] AS T3
WHERE T3.[IncomingDate] BETWEEN (DATEADD(HOUR, -2, @CurrentTime)) AND (DATEADD(HOUR, -1, @CurrentTime))) AS [CountT3]
UNION ALL
--Data from 3 hours
SELECT
----Time
(SELECT CONVERT(VARCHAR(5), (DATEADD(HOUR, -3, @CurrentTime)), 114)) AS [Time],
----Count on 1st table
(SELECT COUNT(T1.[TableFirstId])
FROM [dbo].[Table1] AS T1
WHERE T1.[IncomingDate] BETWEEN (DATEADD(HOUR, -3, @CurrentTime)) AND (DATEADD(HOUR, -2, @CurrentTime))) AS [CountT1],
----Count on 2nd table
(SELECT COUNT(T2.[TableSecondId])
FROM [dbo].[Table2] AS T2
WHERE T2.[IncomingDate] BETWEEN (DATEADD(HOUR, -3, @CurrentTime)) AND (DATEADD(HOUR, -2, @CurrentTime))) AS [CountT2],
----Count on 3rd table
(SELECT COUNT(T3.[TableThirdId])
FROM [dbo].[Table3] AS T3
WHERE T3.[IncomingDate] BETWEEN (DATEADD(HOUR, -3, @CurrentTime)) AND (DATEADD(HOUR, -2, @CurrentTime))) AS [CountT3]
UNION ALL
--(etc...)
那个查询给了我类似的东西:
Time | CountT1 | CountT2 | CountT3
21:05 | 3215467 | 3456364 | 3234234
20:05 | 2253221 | 3123123 | 3238291
19:05 | 1231467 | 1232342 | 1123123
18:05 | 3112412 | 6712353 | 1233124
17:05 | 1242141 | 1241142 | 4112426
16:05 | 3123467 | 3456364 | 3234234
15:05 | 3215467 | 3412334 | 3231234
14:05 | 3324467 | 3456123 | 2312334
13:05 | 3215467 | 3456364 | 1112310
12:05 | 3215467 | 3456364 | 1231234
11:05 | 3123127 | 3456364 | 3234234
10:05 | 3215467 | 3456364 | 3234234
09:05 | 3215467 | 3456364 | 3234234
08:05 | 3215467 | 3456364 | 3234234
07:05 | 3215467 | 3456364 | 3234234
06:05 | 3215467 | 3456364 | 3234234
05:05 | 3215467 | 2212214 | 3234234
04:05 | 3215467 | 3126542 | 3234234
03:05 | 3215467 | 3123364 | 3234234
02:05 | 3215467 | 3456364 | 3234234
01:05 | 3215467 | 3456364 | 3234234
00:05 | 3215467 | 3456364 | 3123123
23:05 | 3215467 | 3456364 | 3212313
22:05 | 3223424 | 1232163 | 1235321
我需要通过更简单的查询返回相同的结果(想到这样的事情):
DECLARE @CurrentTime datetime = GETDATE();
--Data from 24 hours
SELECT
----Count on 1st table
(SELECT COUNT(T1.[TableFirstId])
FROM [dbo].[Table1] AS T1
WHERE T1.[IncomingDate] BETWEEN (DATEADD(HOUR, -24, @CurrentTime)) AND @CurrentTime) AS [CountT1],
----Count on 2nd table
(SELECT COUNT(T2.[TableSecondId])
FROM [dbo].[Table2] AS T2
WHERE T2.[IncomingDate] BETWEEN (DATEADD(HOUR, -24, @CurrentTime)) AND @CurrentTime) AS [CountT2],
----Count on 3rd table
(SELECT COUNT(T3.[TableThirdId])
FROM [dbo].[Table3] AS T3
WHERE T3.[IncomingDate] BETWEEN (DATEADD(HOUR, -24, @CurrentTime)) AND @CurrentTime) AS [CountT3]
FROM [dbo].[Table1] AS T1
GROUP BY DATEPART(HOUR, T1.[IncomingDate])
但它并没有像我预期的那样奏效。是否有人会帮助我理解这一点?并解决我的问题?
答案 0 :(得分:1)
此解决方案围绕将IncomingDate截断为您需要的每小时增量,然后在该值上加入子查询。我正在修改您的要求,并要求所有小时增量完全按小时开始,因为这大大简化了查询。 我截断使用
CONVERT(VARCHAR(13), IncomingDate, 120)
还有其他方法可以这样做(突出显示here)但如果我们要对这个价值进行分组,那么这个方法似乎更好。 要获取一个表的每小时行数,我们有一个这样的查询:
SELECT COUNT(*) AS T1Count,
CONVERT(VARCHAR(13), IncomingDate, 120) AS IncomingDate
FROM Table1
GROUP BY CONVERT(VARCHAR(13), IncomingDate, 120)
现在我们需要做的就是重复所有3个表,然后在T1.IncomingDate上进行FULL OUTER JOIN。结果如下:
SELECT T1.IncomingDate, t1.T1Count, t2.T2Count, t3.T3Count FROM
(SELECT COUNT(*) AS T1Count,
CONVERT(VARCHAR(13), IncomingDate, 120) as IncomingDate
from Table1
group by CONVERT(VARCHAR(13), IncomingDate, 120)) As T1
FULL OUTER JOIN (SELECT COUNT(*) AS T2Count,
CONVERT(VARCHAR(13), IncomingDate, 120) as IncomingDate
from Table2
group by CONVERT(VARCHAR(13), IncomingDate, 120)) As T2
ON T1.IncomingDate = T2.IncomingDate
FULL OUTER JOIN (SELECT COUNT(*) AS T3Count,
CONVERT(VARCHAR(13), IncomingDate, 120) as IncomingDate
from Table3
group by CONVERT(VARCHAR(13), IncomingDate, 120)) As T3
ON T1.IncomingDate = T3.IncomingDate
order by T1.IncomingDate
请注意
答案 1 :(得分:1)
此查询将为您提供与使用较少SQL的查询完全相同的结果。
DECLARE @HourTbl TABLE ( HourBegin DATETIME, HourEnd DATETIME)
DECLARE @i INT=0
DECLARE @dtBegin datetime
DECLARE @dtEnd datetime = GETDATE()
WHILE @i < 24
BEGIN
SET @i = (@i + 1)
SET @dtBegin = @dtEnd
SET @dtEnd = DATEADD(HOUR, -@i, @dtBegin)
INSERT INTO @HourTbl
SELECT @dtBegin, @dtEnd
END
;with hourVal ( hourBegin , hourEnd )
as
(
select hourBegin, hourend
from @HourTbl
)
select CONVERT(VARCHAR(5), hourVal.hourBegin, 114),
(
select COUNT(T1.[TableFirstId])
from Table1
where T1.[IncomingDate] BETWEEN hourVal.hourBegin, hourVal.hourEnd
),
(
select COUNT(T1.[TableFirstId])
from Table2
where T1.[IncomingDate] BETWEEN hourVal.hourBegin, hourVal.hourEnd
)