我需要制作关于公司卡车的一般报告。
我的架构中有这些表:
架构图片:
基本上,我需要创建一个包含以下内容的表:
|地点|卡车| TotalOfCampaings | CampaingsWithCompleteStatus | CampaingsWithInProcessStatus |
广告投放=订购一辆或多辆卡车。
我尝试了内部联接查询,但我无法得到我对一般报告的期望。
我很感激帮助我!
答案 0 :(得分:0)
SELECT *
FROM
-- Prepare the base data for the report
(SELECT location, COUNT(*) AS Trucks FROM Truck GROUP BY location) loc
-- The statistics needed, make sure it is 1 to 1
-- The status value just guess as you did not mention in the question
OUTER APPLY
(
SELECT
COUNT(*) AS TotalOfCampaings,
SUM(CASE WHEN cc.campaing_status = 'Complete' THEN 1 ELSE 0 END) AS CampaingsWithCompleteStatus,
SUM(CASE WHEN cc.campaing_status = 'InProcess' THEN 1 ELSE 0 END) AS CampaingsWithInProcessStatus
FROM CampaingControl cc INNER JOIN Truck t ON cc.vin = t.vin
WHERE t.location = loc.location
) stat