Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Id Grade 1 Grade 2 Grade 3
- 1 1 1 1
- 2 5 0 0
- 3 3 1 5
- Id Grade 1 Grade 2 Grade 3 Total
- 1 1 1 1 3
- 2 5 0 0 8
- 3 3 1 5 17
- select
- t.*,
- @rolling_sum := @rolling_sum + `Grade 1` + `Grade 2` + `Grade 3` AS Total
- from
- Table1 t
- , (select @rolling_sum := 0) var_init
- order by id
- select t.*,
- (select sum([Grade 1] + [Grade 2] + [Grade 3]) from Table1 sub_t where sub_t.id <= t.id)
- from Table1 t
- order by id
- SELECT A.*, (@runtot := @runtot + `Grade 1` + `Grade 2` + `Grade 3`) AS Total
- FROM Table1 A
- ,(SELECT @runtot:=0) c
- ;with cte as (
- select
- id,
- grade1,
- grade2,
- grade3,
- isnull(grade1,0) + isnull(grade2,0) + isnull(grade3,0) as linetotal
- from grade
- )
- select *, total = (select sum(ss.linetotal) from cte ss where ss.id <= cte.id)
- from cte
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement