0


我有两个电表。它们的值被记录到 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

但我不知道如何实现这一点?

编辑:“计数”是一个连续值

4

1 回答 1

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
于 2013-11-08T13:45:19.933 回答