FreddieGericke
FreddieGericke

Reputation: 549

Conditional sum in Group By query MSSQL

I have a table OrderDetails with the 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 a query that will surmise the above table into a new table with the following schema:

----------------------------------------------------------------
|  OrderId  |  ItemCount  |  TotalCopyCost  |  TotalFullPrice  |
----------------------------------------------------------------
|  16       |  4          |  150            |  100             |
----------------------------------------------------------------

Currently I am using a Group By on the Order.Id to the the item count. But I do not know how to conditionally surmise the CopyCost and FullPrice values.

Any help would be much appreciated.

Regards Freddie

Upvotes: 40

Views: 108741

Answers (4)

Verma
Verma

Reputation: 966

You could also try...

select A.OrderID, A.ItemCount,B.TotalCopyCost, C.TotalFullPrice
from (select OrderID, count(*) as ItemCount from orderdetails) as A,
(select OrderID, sum(CopyCost) as TotalCopyCost from orderdetails where PriceType = 'CopyCost') as B,
(select OrderID, sum(FullPrice) as TotalFullPrice from orderdetails where PriceType = 'FullPrice') as C
where A.OrderID = B.OrderID

SQLFiddle: http://sqlfiddle.com/#!2/946af/6

Upvotes: 1

Meherzad
Meherzad

Reputation: 8553

Try this query

select 
   orderId, 
   count(*) as cnt, 
   sum(if(pricetype='CopyCost', CopyCost, 0)) as totalCopyCost,
   sum(if(pricetype='FullPrice', FullPrice, 0)) as totalFullPrice
from 
   tbl
group by 
   orderId

SQL FIDDLE:

| ORDERID | CNT | TOTALCOPYCOST | TOTALFULLPRICE |
--------------------------------------------------
|      16 |   4 |           150 |            100 |

Upvotes: 9

peterm
peterm

Reputation: 92785

Try

SELECT OrderId, 
       COUNT(*) ItemCount,
       SUM(CASE WHEN PriceType = 'CopyCost' THEN Price ELSE 0 END) TotalCopyCost,
       SUM(CASE WHEN PriceType = 'FullPrice' THEN Price ELSE 0 END) TotalFullPrice
  FROM OrderDetails
 GROUP BY OrderId

SQLFiddle

Upvotes: 102

Matt Mitchell
Matt Mitchell

Reputation: 41823

Could you use:

SELECT
  OrderId,
  Count(1) as ItemCount,
  SUM(CASE WHEN PriceType = 'CopyCost' 
        THEN CopyCost ELSE 0 END) AS TotalCopyCost,
  SUM(CASE WHEN PriceType = 'FullPrice' 
        THEN FullPrice ELSE 0 END) AS TotalFullPrice
FROM OrderDetails
GROUP BY OrderId

Upvotes: 4

Related Questions