MSSQL combine SUMS from 2 tables
Below query combines the SUMS of two tables into a total SUM for a specific column.
WITH combiTable ([Table], xName, xValue) AS ( SELECT 1 AS [Table], name, SUM(value) FROM Table1 GROUP BY name UNION SELECT 2 AS [Table], name, SUM(value) FROM Table2 GROUP BY name ) SELECT xName AS Name, SUM(xValue) AS Total FROM combiTable GROUP BY xName ORDER BY Total DESC GO