我有两个电表。它们的值将记录到MSSQL 2008数据库中。
表设计就像
TimeStamp MeterID Count
2013-09-28 00:00:10 BA123 1453.034
2013-09-28 00:01:12 BA123 1454.770
2013-09-28 00:00:14 BB777 2351.000
...
2013-09-28 23:59:50 BA123 1470.120
2013-09-29 00:00:05 BA123 1470.445
我需要每天消费,比如
2013-09-28 BA123 17.411
但我不知道如何实现这个目标?
编辑:“计数”是一个连续值
答案 0 :(得分:1)
您可以将CAST
您的TimeStamp列DATE
分组并对其进行分组:
WITH SampleData AS
(
SELECT [TimeStamp], [MeterID], [Count]
FROM ( VALUES
('2013-09-28 00:00:10', 'BA123', 1453.034),
('2013-09-28 00:01:12', 'BA123', 1454.770),
('2013-09-28 00:00:14', 'BB777', 2351.000),
('2013-09-28 23:59:50', 'BA123', 1470.120),
('2013-09-29 00:00:05', 'BA123', 1470.445)
) AS SampleData([TimeStamp], [MeterID], [Count])
)
SELECT MeterId, CAST([TimeStamp] AS DATE) AS [DateConsumption],
MAX([Count]) - MIN([Count]) AS [CountConsumption]
FROM SampleData
GROUP BY MeterID, CAST([TimeStamp] AS DATE)
--WITH ROLLUP