Search code examples
sqlsql-servert-sqldistinct

SQL Server - show distinct values


Policy ID YTD Value Business Type
1 120 Taxes
1 100 Taxes
1 330 Taxes
2 120 Taxes
2 130 Taxes

I have the table above. What I would like to change it to is

Policy ID YTD Value Business Type
1 550 Taxes
2 250 Taxes

Basically, add up the totals using sum, but combining the PolicyID columns to where they are the same. I've tried using distinct after policy ID and group by and since the other values are different, it still shows each entry separate.

How would I do this?


Solution

  • A simple aggregation can do the trick :

    SELECT Policy_ID, SUM(YTD_Value) AS YTD_Value, Business_Type
    FROM mytable
    GROUP BY Policy_ID, Business_Type
    

    Demo here