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 

sqlfiddle


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -