我从计数器接收数据,我想创建一个报告,打印每小时增加的数量。
我提出了这个问题,但我无法从0到23小时进行选择
SELECT [COLUMN_A],
[COLUMN_B],
[COLUMN_A] - [COLUMN_B] AS TOTAL
FROM (
(SELECT MAX([_Counter]) AS COLUMN_A
FROM [DADOS].[dbo].Table_Producao]
WHERE DatePart(HOUR, _TimeStamp)=9)AS PART_A
CROSS JOIN(SELECT MAX([_Count]) AS COLUMN_B
FROM [DADOS].[dbo].[Table_Producao]
WHERE DatePart(HOUR, _TimeStamp)=8)AS PART_B
)
我有什么:
_TimeStamp _Counter
2015-02-03 14:00:00.000 2
2015-02-03 14:59:00.000 15
2015-02-03 15:00:00.000 17
2015-02-03 15:30:00.000 30
2015-02-03 15:59:00.000 42
2015-02-03 16:00:00.000 43
2015-02-03 16:30:00.000 50
2015-02-03 16:59:00.000 59
2015-02-03 17:00:00.000 61
2015-02-03 17:30:00.000 70
2015-02-03 17:50:00.000 82
2015-02-03 18:00:00.000 86
2015-02-03 18:30:00.000 90
2015-02-03 18:59:00.000 99
2015-02-03 19:00:00.000 102
2015-02-03 19:30:00.000 115
2015-02-03 19:59:00.000 124
2015-02-03 20:00:00.000 126
2015-02-03 20:30:00.000 137
2015-02-03 20:59:00.000 145
2015-02-03 21:00:00.000 147
我想要的是什么:
_TimeStamp _Counter
14h 17
15h 27
16h 17
17h 23
18h 17
19h 25
20h 21
21h 2
有什么想法吗?请
答案 0 :(得分:0)
试试这个。
SELECT Cast(Datepart(hh, [_timestamp]) AS VARCHAR(3))+ 'h',
Sum(_counter)
FROM your_result
GROUP BY Cast(Datepart(hh, [_timestamp]) AS VARCHAR(3))+ 'h',
Cast(_timestamp AS DATE)
答案 1 :(得分:0)
我用这个查询解决了我的问题:
WITH QUERY1 AS
(SELECT MAX(_Counter) AS MAX_1, Datepart(hh, [TimeStamp]) AS _TIMESTAMP1
FROM Table
WHERE Cast(DatePart(hh, TimeStamp)AS int) IN (SELECT Cast(Datepart(hh, [TimeStamp]) AS int)
FROM Table
WHERE TimeStamp>=CAST('<%DataQuery%>'+' 00:00:00.000' AS DATETIME) AND TimeStamp<=CAST('<%DataQuery%>'+' 23:59:59.999' AS DATETIME)
)
GROUP BY Cast(Datepart(hh, [TimeStamp])AS int)),
QUERY2 AS
(SELECT MAX(_Counter) AS MAX_2, Datepart(hh, [TimeStamp]) AS _TIMESTAMP2
FROM PLASCAR_DADOS.dbo.Table_Producao
WHERE Cast(DatePart(hh, TimeStamp)AS int) IN (SELECT Cast(Datepart(hh, [TimeStamp]) AS int)+1
FROM Table
WHERE TimeStamp>=CAST('<%DataQuery%>'+' 00:00:00.000' AS DATETIME) AND TimeStamp<=CAST('<%DataQuery%>'+' 23:59:59.999' AS DATETIME)
)
GROUP BY Cast(Datepart(hh, [TimeStamp])AS int))
SELECT _TIMESTAMP1, MAX_2-MAX_1 AS TOTAL
FROM QUERY1 INNER JOIN QUERY2 ON _TIMESTAMP1 =_TIMESTAMP2-1