Skip to content
Advertisement

Modify query so as to add new row with sum of values in some column

I have table in SQL Server like below using below code:

How can I modify my query in SQL Server so as to add new row with sum of values in col2? So I need something like below:

Advertisement

Answer

You could use ROLLUP for this. The documentation explains how this works. https://docs.microsoft.com/en-us/sql/t-sql/queries/select-group-by-transact-sql?view=sql-server-ver15

—EDIT— Here is the updated code demonstrating how coalesce works.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement