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.
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.