SQL Server中几个表的常规报告

时间:2016-08-26 02:44:01

标签: sql sql-server database report sql-server-2014

我需要创建以下表模式的一般报告:

Table Schema

我需要为常规报告创建以下表格:

+----------+--------+------------------+-----------------------------+------------------------------+
| Location | Trucks | TotalOfCampaings | CampaingsWithCompleteStatus | CampaingsWithInProcessStatus |
+----------+--------+------------------+-----------------------------+------------------------------+
|          |        |                  |                             |                              |
+----------+--------+------------------+-----------------------------+------------------------------+
|          |        |                  |                             |                              |
+----------+--------+------------------+-----------------------------+------------------------------+
  • 位置:卡车位置至少有一次活动。
  • 卡车:按地点划分的卡车数量,至少有一项活动。
  • TotalOfCampaings :位置和卡车的总投放次数。
  • CampaingsWithCompleteStatus :已完成的广告系列总数,状态位于广告系列表中。
  • CampaingsWithInProcessStatus :处于进行中状态的广告投放总数,状态位于广告系列表中。

广告投放=订购一辆或多辆卡车。

我尝试了内部联接,但我无法获得对一般报告的期望。

非常感谢帮助我,关于评论的任何问题!

1 个答案:

答案 0 :(得分:4)

SELECT Truck.location as Location,
    COUNT(Truck.vin) as Trucks,
    COUNT(Campaing.campaing_id) as TotalOfCampaings,
    sum(case when Campaing.campaing_estatus = 'Complete' then 1 else 0 end) as CampaingsWithCompleteStatus,
    sum(case when Campaing.campaing_estatus = 'InProcess' then 1 else 0 end) as CampaingsWithInProcessStatus
    FROM CampaingControl
        INNER JOIN Truck ON CampaingControl.vin = Truck.vin
        INNER JOIN Campaing ON CampaingControl.campaing_id = Campaing.campaing_id
    GROUP BY Truck.location;