我想显示最近一小时的生产数量,每10秒分组一次 到目前为止,我已经完成了这个
SELECT
COUNT(*) AS [PRODUCTION],
(DATEPART(HOUR, DATEADD(SECOND, -(DATEPART(SECOND, TL_Timestamp)) % 10, TL_Timestamp))) AS [HOUR],
(DATEPART(MINUTE, DATEADD(SECOND, -(DATEPART(SECOND, TL_Timestamp)) % 10, TL_Timestamp))) AS [MIN],
(DATEPART(SECOND, DATEADD(SECOND, -(DATEPART(SECOND, TL_Timestamp)) % 10, TL_Timestamp))) AS [FROMSECOND]
FROM tblTransaction
WHERE TL_Timestamp BETWEEN DATEADD(HOUR,-1, GETDATE()) AND GETDATE()
AND TL_IsAccepted = 1
GROUP BY
(DATEPART(HOUR, DATEADD(SECOND, -(DATEPART(SECOND, TL_Timestamp)) % 10, TL_Timestamp))),
(DATEPART(MINUTE, DATEADD(SECOND, -(DATEPART(SECOND, TL_Timestamp)) % 10, TL_Timestamp))),
(DATEPART(SECOND, DATEADD(SECOND, -(DATEPART(SECOND, TL_Timestamp)) % 10, TL_Timestamp)))
ORDER BY
(DATEPART(HOUR, DATEADD(SECOND, -(DATEPART(SECOND, TL_Timestamp)) % 10, TL_Timestamp))) DESC,
(DATEPART(MINUTE, DATEADD(SECOND, -(DATEPART(SECOND, TL_Timestamp)) % 10, TL_Timestamp))) DESC,
(DATEPART(SECOND, DATEADD(SECOND, -(DATEPART(SECOND, TL_Timestamp)) % 10, TL_Timestamp))) DESC
这项工作正常,但如果整个生产过程没有发生10秒钟,则不予考虑。我希望它为零 有什么想法吗?
答案 0 :(得分:2)
您可以创建一个包含所有时间戳组(预填充和固定)的表,并将其作为连接中的第一个表(另一个在左连接中)。
EG。 (在你的情况下,它们应该是360条记录,即一小时,或一整天8640)
ID MIN SEC
1 00 10
2 00 20
3 00 30
...
如果你称之为TL_TIME_DIVISION
SELECT TL_TIME_DIVISION.HOUR, TL_TIME_DIVISION.MIN, TL_TIME_DIVISION.SEC, COALESCE (PRODUCTION,0) AS PRODUCTION
FROM TL_TIME_DIVISION
LEFT JOIN **your query** ON **use Hour, min e sec**