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

php - cannot display multiple markers in google maps v3 from traceroute result -

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

javascript - firefox memory leak -