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 2005 Forums
 Transact-SQL (2005)
 Problem with sum() in T-sql. Please help me!

Author  Topic 

txk2601
Starting Member

19 Posts

Posted - 2009-03-31 : 05:46:50
Hi, I have a problem with T-sql. I have a table as following

ID | Value1 | Value 2 ...
1 | 12 | 23
2 | 24 | 06
3 | 01 | 36
...

Now, i wan't add total row in to this table. as following

ID | Value1 | Value 2 ...
1 | 12 | 23
2 | 24 | 06
3 | 01 | 36
NULL | 37 | 65

I don't know way to make it. Please help me! Thank alot

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-03-31 : 05:54:00
[code]DECLARE @Sample TABLE
(
ID INT,
Value1 INT,
Value2 INT
)

INSERT @Sample
SELECT 1, 12, 23 UNION ALL
SELECT 2, 24, 06 UNION ALL
SELECT 3, 01, 36

SELECT ID,
Value1,
Value2
FROM @Sample

UNION ALL

SELECT NULL,
SUM(Value1),
SUM(Value2)
FROM @Sample[/code]


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-03-31 : 05:55:47
1 Do this in Reports
2 Read about CUBE operator
3
SELECT ID,value1,value2 ...
UNION ALL
SELECT NULL,sum(value1),sum(value2) ...


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-03-31 : 05:56:09


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

txk2601
Starting Member

19 Posts

Posted - 2009-03-31 : 23:11:33
Thanks Peso and Madhivanan very much!
Go to Top of Page
   

- Advertisement -