SQL 中的当前行的列总和?
Sum column up to the current row in SQL?
我正在尝试将一列加到当前行(在 SQL 服务器中)。我该怎么做?
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 ║
╚════════╝
试试这个
删除 "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
这里是计算运行总计
的示例
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;
在 Sql 2012
SELECT ID, val,
RunningTotal = SUM(val) OVER (ORDER BY TID ROWS UNBOUNDED PRECEDING)
FROM @t
ORDER BY TID;
相同的代码可以用在你的查询中我猜 countertime 可以使用 Id's 然后它会按照结果工作
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
我正在尝试将一列加到当前行(在 SQL 服务器中)。我该怎么做?
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 ║
╚════════╝
试试这个
删除 "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
这里是计算运行总计
的示例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;
在 Sql 2012
SELECT ID, val,
RunningTotal = SUM(val) OVER (ORDER BY TID ROWS UNBOUNDED PRECEDING)
FROM @t
ORDER BY TID;
相同的代码可以用在你的查询中我猜 countertime 可以使用 Id's 然后它会按照结果工作
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