如何对total gross使用条件聚合

1rhkuytd  于 2021-08-13  发布在  Java
关注(0)|答案(1)|浏览(254)

我有个问题:

SELECT CAST(co.DateCreated AS DATE) AS Date, 
SUM(w.Gross),
       SUM(CASE WHEN co.BookingSourceId = 1 THEN 1 ELSE 0 END) as Website,
       SUM(CASE WHEN co.BookingSourceId = 2 THEN 1 ELSE 0 END) as Phone,
       COUNT(*) as Total_Orders
FROM [Sterlingbuild].[dbo].[CustomerOrder] co
INNER JOIN ( SELECT Gross, CustomerOrderId, p.ProductBrandId
             FROM [Sterlingbuild].[dbo].[CustomerOrderItem] coi 
             INNER JOIN [Sterlingbuild].[dbo].[Product] p ON coi.ProductId = p.ProductId
             ) w ON co.CustomerOrderId = w.CustomerOrderId
WHERE CustomerOrderStatusId = 7 AND DepartmentId = 1 AND w.ProductBrandId = 7
GROUP BY CAST(co.DateCreated AS DATE)
ORDER BY CAST(co.DateCreated AS DATE)

目前,它返回的电话/网站的订单数量,但我想总毛为两个电话/网站我该如何修改查询来实现这一点。
使用sql server

rdlzhqv9

rdlzhqv91#

求和 gross :

SUM(CASE WHEN co.BookingSourceId = 1 THEN w.gross ELSE 0 END) as Website_gross,
   SUM(CASE WHEN co.BookingSourceId = 2 THEN w.gross ELSE 0 END) as Phone_gross,

相关问题