如何总结和Group By Union选择查询的结果

时间:2014-10-29 11:50:32

标签: sql-server-2008 select sum

我有一个查询,其中Union的两个单独的查询具有相同的字段/数据类型。查询如下:

SELECT     BusinessUnitName, BuildingNumber, Description, Value_1, 
                  LifeRemaining, Sum_Quant
FROM         
(
SELECT     bu.BusinessUnitName, b.BuildingNumber, ec.Description, SUM(cc.MonetaryValue) AS Value_1, 
                  cc.LifeRemaining, SUM(a.Quantity) AS Sum_Quant
FROM         tbBuildingLinkBusinessUnit as blb INNER JOIN
                  tbBusinessUnit as bu ON blb.BusinessUnitID = bu.BusinessUnitID INNER JOIN
                  tbBuilding as b ON blb.BuildingID = b.BuildingID INNER JOIN
                  tbFloor ON b.BuildingID = tbFloor.BuildingID INNER JOIN
                  tbRoom as r ON tbFloor.FloorID = r.FloorID INNER JOIN
                  tbConditionComponent as cc INNER JOIN
                  tbAsset as a ON cc.ParentID = a.AssetUID INNER JOIN
                  tbElement as e ON cc.ElementID = e.ElementID AND a.ElementID = e.ElementID INNER JOIN
                  tbElementCategory as ec ON e.ElementCategoryID = ec.ElementCategoryID ON r.RoomID = a.LocationID
WHERE     (cc.MonetaryValue > 0)
GROUP BY bu.BusinessUnitName, b.BuildingNumber, ec.Description, a.Status, cc.LifeRemaining
HAVING      (a.Status = 0)

UNION 

SELECT     bu.BusinessUnitName, b.BuildingNumber, ec.Description, SUM(cc.MonetaryValue) AS Value_1, 
                  cc.LifeRemaining, SUM(a.Quantity) AS Sum_Quant
FROM         tbBuildingLinkBusinessUnit as blb INNER JOIN
                  tbBusinessUnit as bu ON blb.BusinessUnitID = bu.BusinessUnitID INNER JOIN
                  tbBuilding as b ON blb.BuildingID = b.BuildingID INNER JOIN
                  tbConditionComponent as cc INNER JOIN
                  tbAsset as a ON cc.ParentID = a.AssetUID INNER JOIN
                  tbElement as e ON cc.ElementID = e.ElementID AND a.ElementID = e.ElementID INNER JOIN
                  tbElementCategory as ec  ON e.ElementCategoryID = ec.ElementCategoryID ON b.BuildingID = a.LocationID
WHERE     (cc.MonetaryValue > 0)
GROUP BY bu.BusinessUnitName, b.BuildingNumber, ec.Description, a.Status, cc.LifeRemaining
HAVING      (a.Status = 0)
) AS x

ORDER BY BusinessUnitName, Description

单个选择查询的结果如下,前两行来自查询1,后两行来自查询2:

测试物业| 1 / A |电气服务| 515.82 | 0 | 3

测试物业| 1 / A |电气服务| 125 | 1 | 2

测试物业| 1 / A |电气服务| 381.6 | 0 | 8

测试物业| 1 / A |电气服务| 80615.93 | 5 | 7

我的问题是我现在如何合并两个查询的结果,以便两个查询的第一个结果执行SUM,因为它们在第5列中都具有值0?这将产生3行结果,其中第1行和第3行合并。

提前致谢

1 个答案:

答案 0 :(得分:2)

使用

         Derived GROUP BY your_value

尝试如下,

          SELECT     BusinessUnitName, BuildingNumber, Description, 
          LifeRemaining, SUM(Value_1) as Value, SUM(Sum_Quant) as Quant
          FROM         
          (
          SELECT     bu.BusinessUnitName, b.BuildingNumber, ec.Description, SUM(cc.MonetaryValue)          AS Value_1, 
              cc.LifeRemaining, SUM(a.Quantity) AS Sum_Quant
          FROM         tbBuildingLinkBusinessUnit as blb INNER JOIN
              tbBusinessUnit as bu ON blb.BusinessUnitID = bu.BusinessUnitID INNER JOIN
              tbBuilding as b ON blb.BuildingID = b.BuildingID INNER JOIN
              tbFloor ON b.BuildingID = tbFloor.BuildingID INNER JOIN
              tbRoom as r ON tbFloor.FloorID = r.FloorID INNER JOIN
              tbConditionComponent as cc INNER JOIN
              tbAsset as a ON cc.ParentID = a.AssetUID INNER JOIN
              tbElement as e ON cc.ElementID = e.ElementID AND a.ElementID = e.ElementID INNER            JOIN
              tbElementCategory as ec ON e.ElementCategoryID = ec.ElementCategoryID ON r.RoomID =  a.LocationID
              WHERE     (cc.MonetaryValue > 0)
              GROUP BY bu.BusinessUnitName, b.BuildingNumber, ec.Description, a.Status,            cc.LifeRemaining
              HAVING      (a.Status = 0)

             UNION 

            SELECT     bu.BusinessUnitName, b.BuildingNumber, ec.Description, SUM(cc.MonetaryValue) AS Value_1, 
              cc.LifeRemaining, SUM(a.Quantity) AS Sum_Quant
           FROM         tbBuildingLinkBusinessUnit as blb INNER JOIN
              tbBusinessUnit as bu ON blb.BusinessUnitID = bu.BusinessUnitID INNER JOIN
              tbBuilding as b ON blb.BuildingID = b.BuildingID INNER JOIN
              tbConditionComponent as cc INNER JOIN
              tbAsset as a ON cc.ParentID = a.AssetUID INNER JOIN
              tbElement as e ON cc.ElementID = e.ElementID AND a.ElementID = e.ElementID INNER  JOIN
              tbElementCategory as ec  ON e.ElementCategoryID = ec.ElementCategoryID ON      b.BuildingID = a.LocationID
              WHERE     (cc.MonetaryValue > 0)
                GROUP BY bu.BusinessUnitName, b.BuildingNumber, ec.Description, a.Status,           cc.LifeRemaining
              HAVING      (a.Status = 0)
               ) Derived GROUP BY BusinessUnitName, BuildingNumber, Description, 
          LifeRemaining                   
              ORDER BY BusinessUnitName, Description

供参考 https://social.msdn.microsoft.com/forums/sqlserver/en-US/cd32bf58-c581-404b-a384-e62cdda7a131/union-all-and-group-by-query

希望它有所帮助...