22,209
社区成员
发帖
与我相关
我的任务
分享
;WITH Line(ID,Title) AS (
select 1,'a' UNION ALL
SELECT 2, 'b' UNION ALL
SELECT 3,'c'
),LineOrder(Id,LineTitle,LineId,OrderStatus,TotalPrice) AS (
select 1,'a',1,'s;',100 UNION ALL
select 2,'a',1,'f',120 UNION ALL
select 3,'b',2,'f',150 UNION ALL
SELECT 4,'b',2,'s',155 UNION ALL
select 5,'c',3,'s',250 UNION ALL
SELECT 6,'c',3,'f',255 UNION ALL
SELECT 7,'c',3,'f',200
),LineOrderPerson(ID,OrderId) AS (
select 1,4 UNION ALL
select 2,6 UNION ALL
select 3,1 UNION ALL
select 4,7
)
select l.Id,l.Title,count(0) as SuccedOrderCount
,sum(case WHEN lop.id is null then 0 else 1 END )as LinePersonCount
,sum(lo.TotalPrice) as LineTotalPrice
from Line as l LEFT JOIN LineOrder as lo on lo.LineId=l.ID
left join LineOrderPerson as lop on lop.OrderId=lo.Id AND lo.OrderStatus='s'
GROUP BY l.Id,l.Title