SQL Query to get the Sum of all column values in the last row of a resultset along with row sum (group by)

mysql, sql, sql-server, sql-server-2008

Solution

You can use `GROUP BY` and `WITH ROLLUP`, like this:

SELECT
    id
,   SUM(jan) as jan
,   SUM(feb) as feb
,   SUM(mar) as mar
,   SUM(jan+feb+mar) as TRS
FROM test
GROUP BY id WITH ROLLUP

Live demo on sqlfiddle.

Problem

Can someone please help me to write a query to obtain the TCS and TRS? ``` ID Jan Feb Mar TRS 1 4 5 6 15 2 5 5 5 15 3 1 1 1 3 TCS 10 11 12 ``` Both TCS (Total Column Sum) and TRS (Total Row Sum) are new column and row respectively which gives their.

Original source