Advertisement
Guest User

Untitled

a guest
Apr 21st, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. Id Grade 1 Grade 2 Grade 3
  2. 1 1 1 1
  3. 2 5 0 0
  4. 3 3 1 5
  5.  
  6. Id Grade 1 Grade 2 Grade 3 Total
  7. 1 1 1 1 3
  8. 2 5 0 0 8
  9. 3 3 1 5 17
  10.  
  11. select
  12. t.*,
  13. @rolling_sum := @rolling_sum + `Grade 1` + `Grade 2` + `Grade 3` AS Total
  14. from
  15. Table1 t
  16. , (select @rolling_sum := 0) var_init
  17. order by id
  18.  
  19. select t.*,
  20. (select sum([Grade 1] + [Grade 2] + [Grade 3]) from Table1 sub_t where sub_t.id <= t.id)
  21. from Table1 t
  22. order by id
  23.  
  24. SELECT A.*, (@runtot := @runtot + `Grade 1` + `Grade 2` + `Grade 3`) AS Total
  25. FROM Table1 A
  26. ,(SELECT @runtot:=0) c
  27.  
  28. ;with cte as (
  29. select
  30. id,
  31. grade1,
  32. grade2,
  33. grade3,
  34. isnull(grade1,0) + isnull(grade2,0) + isnull(grade3,0) as linetotal
  35. from grade
  36. )
  37. select *, total = (select sum(ss.linetotal) from cte ss where ss.id <= cte.id)
  38. from cte
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement