sql - Conditional sum in Group By query MSSQL -
i have table orderdetails following schema:
---------------------------------------------------------------- | orderid | copycost | fullprice | price | pricetype | ---------------------------------------------------------------- | 16 | 50 | 100 | 50 | copycost | ---------------------------------------------------------------- | 16 | 50 | 100 | 100 | fullprice | ---------------------------------------------------------------- | 16 | 50 | 100 | 50 | copycost | ---------------------------------------------------------------- | 16 | 50 | 100 | 50 | copycost | ----------------------------------------------------------------
i need query surmise above table new table following schema:
---------------------------------------------------------------- | orderid | itemcount | totalcopycost | totalfullprice | ---------------------------------------------------------------- | 16 | 4 | 150 | 100 | ----------------------------------------------------------------
currently using group on order.id the item count. not know how conditionally surmise copycost , fullprice values.
any appreciated.
regards freddie
try
select orderid, count(*) itemcount, sum(case when pricetype = 'copycost' price else 0 end) totalcopycost, sum(case when pricetype = 'fullprice' price else 0 end) totalfullprice orderdetails group orderid
Comments
Post a Comment