Answer the question
In order to leave comments, you need to log in
How to get the total value when using running totals?
Task: Calculate running totals for the summa field. Each line should contain the sum of the previous lines without the current value. If you use a window function with parameters: rows between unbounded preceding and current row
totals are counted, including the current line, so I came to the conclusion that you need to count all lines up to the current one:
select [id], [data], [summa],
sum([summa]) over (order by [id]
rows between unbounded preceding and 1 preceding )
as [Itog]
from [dbo].[Operations]
Answer the question
In order to leave comments, you need to log in
I don’t know how in T-SQL, but in oracle you can do this
1. UNION
select id, sum(summa) over (order by id rows between unbounded preceding and 1 preceding ) as Itog from [dbo].[Operations]
union all
select NULL, SUM(summa) from [dbo].[Operations]
select [id], [data], [summa],
sum([summa]) over (order by [id]
rows between unbounded preceding and 1 preceding )
as [Itog],
sum([summa]) over ( ) as [Itog1]
from [dbo].[Operations]
select d1.id, SUM(d2.summa)
from [dbo].[Operations] d1 left join [dbo].[Operations] d2
ON( d1.id > d2.id )
group by rollup(d1.id)
order by d1.id
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question