Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Asking Query to sum Total

Author  Topic 

wilang
Starting Member

12 Posts

Posted - 2014-09-10 : 23:38:28
Value Total
1 1
3 4
4 8
5 13
2 15

Hi All, i have a question about query. i have table with 1 column (value) . i want add some column addition which sum value like example above, but i don't know how to do it. maybe you have a clue or some guidance for me to this query.





W

VeeranjaneyuluAnnapureddy
Posting Yak Master

169 Posts

Posted - 2014-09-11 : 03:02:28
DECLARE @Table Table(Col INT)
INSERT INTO @Table
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 UNION ALL
SELECT 6 UNION ALL
SELECT 7 UNION ALL
SELECT 8 UNION ALL
SELECT 9 UNION ALL
SELECT 10

;WITH CTE AS
(
SELECT
ROW_NUMBER() OVER(ORDER BY Col) AS Row,
Col
FROM
@Table
)
SELECT DISTINCT
C.Col,
SUM(C1.Col) AS Sum
FROM
CTE AS C
LEFT JOIN
CTE AS C1 ON C1.Row <= C.Row
GROUP BY C.Col
ORDER BY C.Col ASC

Veera
Go to Top of Page

djj55
Constraint Violating Yak Guru

352 Posts

Posted - 2014-09-11 : 07:05:13
quote:
Originally posted by wilang
Hi All, i have a question about query. i have table with 1 column (value) . i want add some column addition which sum value like example above, but i don't know how to do it. maybe you have a clue or some guidance for me to this query.
W


This is called a running total. There are many ways to do this.

djj
Go to Top of Page
   

- Advertisement -