【问题标题】:Sum column up to the current row in SQL?将列总和到 SQL 中的当前行?
【发布时间】:2015-04-17 18:00:09
【问题描述】:

我正在尝试将一列汇总到当前行(在 SQL Server 中)。我该怎么做?

select t1.CounterTime,
       t1.StartTime,
       t1.EndTime,
       isNull(t1.value, 0) as value1,

       -- How do I make Total1 the sum of t1.value over all previous rows?
       sum( isNull(t1.value, 0) ) over (partition by t1.CounterTime order by t1.CounterTime) as Total1

from   SomeTable t1
order by t1.CounterTime

但是我弄错了partition by...

╔═══╦═════════════════════════╦═════════════════════════╦═════════════════════════╦════════╦════════╗
║   ║      CounterTime        ║        StartTime        ║         EndTime         ║ value1 ║ Total1 ║
╠═══╬═════════════════════════╬═════════════════════════╬═════════════════════════╬════════╬════════╣
║ 1 ║ 2015-03-17 12:00:00.000 ║ 2015-03-17 00:00:00.000 ║ 2015-03-18 00:00:00.000 ║     0  ║     0  ║
║ 2 ║ 2015-03-18 12:00:00.000 ║ 2015-03-18 00:00:00.000 ║ 2015-03-19 00:00:00.000 ║     0  ║     0  ║
║ 3 ║ 2015-03-19 12:00:00.000 ║ 2015-03-19 00:00:00.000 ║ 2015-03-20 00:00:00.000 ║   422  ║   422  ║
║ 4 ║ 2015-03-20 12:00:00.000 ║ 2015-03-20 00:00:00.000 ║ 2015-03-21 00:00:00.000 ║   989  ║   989  ║
║ 5 ║ 2015-03-21 12:00:00.000 ║ 2015-03-21 00:00:00.000 ║ 2015-03-22 00:00:00.000 ║  1162  ║  1162  ║
║ 6 ║ 2015-03-22 12:00:00.000 ║ 2015-03-22 00:00:00.000 ║ 2015-03-23 00:00:00.000 ║   524  ║   524  ║
╚═══╩═════════════════════════╩═════════════════════════╩═════════════════════════╩════════╩════════╝

应该是:

╔════════╗
║ Total1 ║
╠════════╣
║     0  ║
║     0  ║
║   422  ║
║  1411  ║
║  2573  ║
║  3097  ║
╚════════╝

【问题讨论】:

  • 您使用的是哪个版本的SQL Server
  • 也许使用相关子查询来做 SUM?
  • 这可能非常相关,如果不是重复的话:stackoverflow.com/questions/860966/…
  • 如果你想要之前所有行的总和,只需删除 partition by t1.CounterTime 并保留 order by t1.CounterTime

标签: sql sql-server sum partitioning


【解决方案1】:

试试这个
删除“partition by”只使用“order by”

select t1.CounterTime,
           t1.StartTime,
           t1.EndTime,
           isNull(t1.value, 0) as value1,

           -- How do I make Total1 the sum of t1.value over all previous rows?
           sum( isNull(t1.value, 0) ) over (order by t1.CounterTime) as Total1

    from   SomeTable t1
    order by t1.CounterTime

【讨论】:

    【解决方案2】:

    这里是一个计算运行总数的示例

    declare @t table (id int,val int)
    insert into @t(id,val)values (1,0),(2,0),(3,411),(4,989),(5,1162),(6,524)
    
    SELECT ID, val, RunningTotal = val + COALESCE(
    (
      SELECT SUM(val)
        FROM @t AS i
        WHERE i.ID < o.ID), 0
    )
    FROM @t AS o
    ORDER BY ID;
    

    IN Sql 2012

    SELECT ID, val, 
      RunningTotal = SUM(val) OVER (ORDER BY TID ROWS UNBOUNDED PRECEDING)
    FROM @t
    ORDER BY TID;
    

    可以在您的查询中使用相同的代码

    select t1.CounterTime,
               t1.StartTime,
               t1.EndTime,
               isNull(t1.value, 0) as value1,
               RunningTotal = t1.value + COALESCE(
    (
      SELECT SUM(value)
        FROM SomeTable AS i
        WHERE i.CounterTime < o.CounterTime), 0
    )
    
        from   SomeTable t1
        order by t1.CounterTime
    

    【讨论】:

      猜你喜欢
      • 2022-12-20
      • 1970-01-01
      • 1970-01-01
      • 2012-09-22
      • 2023-01-19
      • 1970-01-01
      • 1970-01-01
      • 2013-03-10
      • 2020-08-01
      相关资源
      最近更新 更多