2014-10-29 82 views
1

我有一個查詢哪些聯盟的兩個單獨的查詢具有相同的字段/數據類型。查詢如下:是如何總結和分組通過聯盟選擇查詢的結果

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來如下:

TEST PROPERTY | 1/A |電氣服務| 515.82 | 0 | 3

TEST PROPERTY | 1/A |電氣服務| 125 | 1 | 2

TEST PROPERTY | 1/A |電氣服務| 381.6 | 0 | 8

TEST PROPERTY | 1/A |電氣服務| 80615.93 | 5 | 7

我的問題是我怎麼現在合併這兩個查詢的結果,以便這兩個查詢的第一個結果執行一個SUM,因爲它們都在列5中的值爲0?這將導致3行結果與第1行和第3行相結合。

在此先感謝

回答

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

希望它可以幫助...

+0

謝謝,我現在試試 – 2014-10-29 12:16:40

+0

我收到以下錯誤: 消息8120,級別16,狀態1,行1 列'Derived.BusinessUnitName'在選擇列表中無效,因爲它不包含在聚合函數或GROUP BY子句。 如果我通過獲得相同的結果包含派生組中的選擇字段? – 2014-10-29 12:21:17

+0

在group by子句中包含BusinessUnitName列,它解決了問題 – SDK 2014-10-29 12:25:16