Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Enterprise
Expertise: Intermediate
Dec 14, 2001

Handling Cumulative Totals in T-SQL


Suppose you have a table:
 
select * from tblTest

Name	Amount
---	------
A	10
B	20
c	40
D	50
E	30

The following single SELECT query can be used to calculate cumulative totals:
 
--//////////////////////////////////////

SELECT t1.Name, t1.Amount,
(SELECT SUM(t2.Amount ) FROM tblTest AS t2 WHERE t2.Name <= t1.Name ) AS
Total
FROM tblTest AS t1

--//////////////////////////////////////

Name	Amount	Total
---	------	-----
A	10	10
B	20	30
c	40	70
D	50	120
E	30	150
Harinatha Reddy
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date