SQL Recipes
A FREE cookbook for SQL queries and examples
Register FAQ Search Today's Posts Mark Forums Read

SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it.

Go Back   SQL Recipes a FREE cookbook of SQL queries and examples > SQL queries and examples > SQL - Questions and Answers

Reply
 
LinkBack Thread Tools Search this Thread
  #1 (permalink)  
Old 05-24-2006, 12:36 AM
alexfr alexfr is offline
Junior Member
 
Join Date: May 2006
Posts: 24
alexfr is on a distinguished road
Default SQL 92 dialect question:

Create a third row that make the sum of past row + current row (i.e. a running sum)?


EXAMPLE :
WEEK TOTAL T
----------- ----------- -----------
3 5 14
2 6 8
1 5 3
0 3 0
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 05-24-2006, 12:59 AM
alexfr alexfr is offline
Junior Member
 
Join Date: May 2006
Posts: 24
alexfr is on a distinguished road
Default SQL 92 answer. Re: Create a third row that make the sum of past row + current row (i.e. a running sum)?

SELECT T2.WEEK, T2.TOTAL, COALESCE(SUM(T1.TOTAL), 0) AS T
FROM MY_TABLE T1
       LEFT OUTER JOIN MY_TABLE T2
             ON T1.WEEK < T2.WEEK
GROUP BY T2.WEEK, T2.TOTAL
ORDER BY 1 DESC


Here I use the COALESCE function that will replace the NULL that can be found in some rows by 0 (otherwise the sum couldn't be done)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-24-2006, 02:59 AM
alexfr alexfr is offline
Junior Member
 
Join Date: May 2006
Posts: 24
alexfr is on a distinguished road
Default SQL 99 answer. Re: Create a third row that make the sum of past row + current row (i.e. a running sum)?

Nearly the same but last column is current total + previous like :
week total t
1 5 5
2 6 11
3 5 16
SELECT t2.week, t2.total, sum(t1.total)
FROM  MY_TABLE T1,  MY_TABLE T2
 WHERE T1.WEEK <=t2.week
GROUP BY  t2.week, T2.total
ORDER BY t2.week ASC
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-21-2006, 07:43 PM
delostilos
 
Posts: n/a
Default SQL 99 answer. Re: Create a third row that make the sum of past row + current row (i.e. a running sum)?

The following feature outside Core SQL-99 is used:

T611, "Elementary OLAP operations"
(checked with the mimer sql validator)
SELECT week
     , total
     , sum(total) over (ORDER BY week DESC) AS running_total
FROM my_table
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 02:48 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright (c) 2006-2008 SQL Recipes

1 2 3 4 5 6 7 8