Sql server 不同时间的汇总计算

Sql server 不同时间的汇总计算,sql-server,group-by,totals,Sql Server,Group By,Totals,我有以下示例,我想计算每个可用时间的总计: timestamp device value 2010-12-30 00:00 1 5 2010-12-30 00:05 1 5 2010-12-30 00:05 2 10 2010-12-30 00:13 1 23 2010-12-30 00:16 3 11 2010-12-30 00:30 1 22 2010-12-30 00:40 2 55 2010-12-30 00:40 3 12 2010-12-30 00:45 2 12 2010-12-

我有以下示例,我想计算每个可用时间的总计:

timestamp  device value
2010-12-30 00:00 1 5
2010-12-30 00:05 1 5
2010-12-30 00:05 2 10
2010-12-30 00:13 1 23
2010-12-30 00:16 3 11
2010-12-30 00:30 1 22
2010-12-30 00:40 2 55
2010-12-30 00:40 3 12
2010-12-30 00:45 2 12
2010-12-30 10:00 3 33
最后,结果应如下所示:

timestamp  Total
2010-12-30 00:00 5
2010-12-30 00:05 5
2010-12-30 00:05 15
2010-12-30 00:13 33
2010-12-30 00:16 44
2010-12-30 00:30 43
2010-12-30 00:40 88
2010-12-30 00:40 89
2010-12-30 00:45 46
2010-12-30 10:00 67
其思想是,对于每个时间戳,我需要得到每个设备的最后一个值,然后将这些值相加

例如:

对于时间戳
2010-12-30 00:13
我需要获取以下条目并求和:

2010-12-30 00:05 2 10
2010-12-30 00:13 1 23
总数将是26。对于时间戳
2010-12-30 10:00

2010-12-30 00:30 1 22
2010-12-30 00:45 2 12
2010-12-30 10:00 3 33
总数是67


我的想法是创建一个查询,为每个设备的每个时间戳选择最新的值,但我已经无法为每个设备的特定时间戳选择最新的值,因此我希望获得一些支持。

这里有一种方法。查询从表中选择所有行,然后使用
outer apply
汇总每个设备的最后一个值。
不存在
查询筛选出不是该设备最后一行的行

select  t1.timestamp
,       last_rows_per_device.Total
from    @t t1
outer apply
        (
        select  sum(t2.value) as Total
        from    @t t2
        where   (
                    t2.timestamp < t1.timestamp
                    or (t2.timestamp = t1.timestamp and t2.device <= t1.device)
                ) 
                and not exists
                (
                    select  *
                    from    @t t3
                    where   t3.device = t2.device
                            and t2.timestamp < t3.timestamp 
                            and t3.timestamp <= t1.timestamp
                )
        ) last_rows_per_device
order by
        t1.timestamp
,       t1.device
来源数据:

declare @t table (timestamp datetime, device int, value int)
insert @t (timestamp, device, value)
          select '2010-12-30 00:00', 1, 5
union all select '2010-12-30 00:05', 1, 5
union all select '2010-12-30 00:05', 2, 10
union all select '2010-12-30 00:13', 1, 23
union all select '2010-12-30 00:16', 3, 11
union all select '2010-12-30 00:30', 1, 22
union all select '2010-12-30 00:40', 2, 55
union all select '2010-12-30 00:40', 3, 12
union all select '2010-12-30 00:45', 2, 12
union all select '2010-12-30 10:00', 3, 33

这里有一种方法。查询从表中选择所有行,然后使用
outer apply
汇总每个设备的最后一个值。
不存在
查询筛选出不是该设备最后一行的行

select  t1.timestamp
,       last_rows_per_device.Total
from    @t t1
outer apply
        (
        select  sum(t2.value) as Total
        from    @t t2
        where   (
                    t2.timestamp < t1.timestamp
                    or (t2.timestamp = t1.timestamp and t2.device <= t1.device)
                ) 
                and not exists
                (
                    select  *
                    from    @t t3
                    where   t3.device = t2.device
                            and t2.timestamp < t3.timestamp 
                            and t3.timestamp <= t1.timestamp
                )
        ) last_rows_per_device
order by
        t1.timestamp
,       t1.device
来源数据:

declare @t table (timestamp datetime, device int, value int)
insert @t (timestamp, device, value)
          select '2010-12-30 00:00', 1, 5
union all select '2010-12-30 00:05', 1, 5
union all select '2010-12-30 00:05', 2, 10
union all select '2010-12-30 00:13', 1, 23
union all select '2010-12-30 00:16', 3, 11
union all select '2010-12-30 00:30', 1, 22
union all select '2010-12-30 00:40', 2, 55
union all select '2010-12-30 00:40', 3, 12
union all select '2010-12-30 00:45', 2, 12
union all select '2010-12-30 10:00', 3, 33

如果您发布代码或XML,请在文本编辑器中突出显示这些行,并单击编辑器工具栏上的“代码示例”按钮({}),以很好地格式化和语法突出显示它!第一个总数——时间戳2010-12-30 00:13——不应该是33吗??您的意思是将10和23相加为值,对吗?数据库中有多少行?性能是个问题吗?我不明白你想做什么。。。。。为什么00:05和00:40有两个结果不同的输出行?对不起,marc_s,是的,应该是33。我只举了一个例子。可能存在不同设备具有相同时间戳的情况。我只是举个例子。在实表中有一个序列字段,它是主键。我想计算表中每个时间戳的所有可用设备的值之和。如果发布代码或XML,请在文本编辑器中突出显示这些行,然后单击编辑器工具栏上的“代码示例”按钮({}),以很好地格式化和语法突出显示它!第一个总数——时间戳2010-12-30 00:13——不应该是33吗??您的意思是将10和23相加为值,对吗?数据库中有多少行?性能是个问题吗?我不明白你想做什么。。。。。为什么00:05和00:40有两个结果不同的输出行?对不起,marc_s,是的,应该是33。我只举了一个例子。可能存在不同设备具有相同时间戳的情况。我只是举个例子。在实表中有一个序列字段,它是主键。我想计算表中每个时间戳上所有可用设备的值之和。