Search code examples
sqlt-sqlgroup-byaggregate-functionssubtotal

Create Subtotal Rows from Group but Remove the detail rows via TSQL


I basically have:

select a,b,c,d,e from table1

I need the subtotal of e where c and d are the same. I suppose we can do a group by c and d but will run into a

"..not part of aggregate function"

issue for columns a and b are not in an aggregate function or part of the group by clause.

Another thing, I only need a row with the subtotal result and not the rows that made up the subtotal


Solution

  • SELECT SUM(CASE WHEN c=d THEN e ELSE 0 END) as subTotal
    FROM tableName