Group By条件求和MSSQL查询

时间:2022-09-21 08:00:30

I have a table OrderDetails with the following schema:

我有一个表OrderDetails与以下架构:

----------------------------------------------------------------
|  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.

目前我在Order.Id上使用Group By来计算项目数。但我不知道如何有条理地推测CopyCost和FullPrice值。

Any help would be much appreciated.

任何帮助将非常感激。

Regards Freddie

关心弗雷迪

4 个解决方案

#1


54  

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

SQLFiddle

#2


4  

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 |

#3


3  

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

#4


0  

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

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

#1


54  

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

SQLFiddle

#2


4  

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 |

#3


3  

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

#4


0  

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

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