question

HJM avatar image
HJM asked

Getting a sum of data from two rows of the same table and to display in that same column in another row

Getting a sum of data from two rows of the same table and to display in that same column in another row.. below is sample table .. Please refer and give your assistance.(without using update function, since before this query union function is presents) column1 column2 **TOTAL 50 40** ABC 30 10 XYZ 20 30 I need the table to update like this ... whenever the data changes in column1 rows ... they total row also need to get that update/...Please help . Thanks In Advance.
sum
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

· Write an Answer
Magnus Ahlkvist avatar image
Magnus Ahlkvist answered
You could either use a UNION ALL operator, to combine the detailed lines with an aggregate. SELECT key,c1,c2 FROM t UNION ALL SELECT 'Total', sum(c1), sum(c2) FROM t You could also use ROLLUP in the GROUP BY clause to get it all in one go. SELECT COALESCE(key, 'Total') as key, sum(c1) as c1, sum(c2) as c2 FROM t GROUP BY ROLLUP(key) If there are more than these Three columns in the table, so you want a more advanced Query, have a look at the technet article about ROLLUP, CUBE and GROUPING SETS. http://technet.microsoft.com/en-us/library/bb522495(v=sql.105).aspx
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.