2016-07-22 39 views
-1

我一直在改善我的查詢性能。我的查詢以單行形式返回每個產品的所有54周基準數據,如下圖所示。我被困在改善我的SQL查詢的性能

SELECT BusinessPlanMain.Id, BusinessPlanMain.SalesOrg, ProductMain.MetaType, ProductMain.Id [Product ID], BplText.Text, BusinessPlanData.Week1, BusinessPlanData.Week2 
       . 
       . 
       . 
, BusinessPlanData.Week54, SystemPeriod.Year 
    FROM BusinessPlanMain INNER JOIN SystemPeriod ON BusinessPlanMain.SystemPeriodPKey = SystemPeriod.PKey INNER JOIN BusinessPlanData ON BusinessPlanMain.PKey = BusinessPlanData.BusinessPlanMainPKey INNER JOIN BusinessPlanDataTemplate ON BusinessPlanDataTemplate.PKey = BusinessPlanData.BusinessPlanDataTemplatePKey INNER JOIN BplText ON BplText.ReferencePKey=BusinessPlanTemplate.PKey INNER JOIN ProductMain ON BusinessPlanData.ProductMainPKey=ProductMain.PKey WHERE SystemPeriod.Year IN ('2016') AND SystemPeriod.PeriodType = 'Year' AND SystemPeriod.SalesOrg = 'OrgID1' AND BusinessPlanMain.SalesOrg='OrgID1' AND BusinessPlanData.OrgID ='OrgID1' AND BusinessPlanTemplate.Sheet='Baseline' and ProductMain.MetaType IN ('PrdGroup','Product') 

The Result of the above Query is

我在我的項目來創建一個數據視圖,其中i有以調換所有54周列數據到單個列作爲如下所示的我已經通過使用所取得的圖像中的有一個要求上述查詢的'Union All'關鍵字54次。

我在尋找更好的解決方案來提高我的查詢性能並忽略了54次聯合。在此先感謝,任何更好的解決方案,可以讚賞。

SELECT BusinessPlanMain.Id 
, BusinessPlanMain.SalesOrg 
, ProductMain.MetaType 
, ProductMain.Id [Product ID] 
, BplText.Text 
, BusinessPlanData.Week1 
, SystemPeriod.Year 
    FROM BusinessPlanMain INNER JOIN SystemPeriod ON BusinessPlanMain.SystemPeriodPKey = SystemPeriod.PKey INNER JOIN BusinessPlanData ON BusinessPlanMain.PKey = BusinessPlanData.BusinessPlanMainPKey INNER JOIN BusinessPlanDataTemplate ON BusinessPlanDataTemplate.PKey = BusinessPlanData.BusinessPlanDataTemplatePKey INNER JOIN BplText ON BplText.ReferencePKey=BusinessPlanTemplate.PKey INNER JOIN ProductMain ON BusinessPlanData.ProductMainPKey=ProductMain.PKey WHERE SystemPeriod.Year IN ('2016') AND SystemPeriod.PeriodType = 'Year' AND SystemPeriod.SalesOrg = 'OrgID1' AND BusinessPlanMain.SalesOrg='OrgID1' AND BusinessPlanData.OrgID ='OrgID1' AND BusinessPlanTemplate.Sheet='Baseline' and ProductMain.MetaType IN ('PrdGroup','Product') Union ALL SELECT BusinessPlanMain.Id 
, BusinessPlanMain.SalesOrg 
, ProductMain.MetaType 
, ProductMain.Id [Product ID] 
, BplText.Text 
, BusinessPlanData.Week2 
, SystemPeriod.Year 
    FROM BusinessPlanMain INNER JOIN SystemPeriod ON BusinessPlanMain.SystemPeriodPKey = SystemPeriod.PKey INNER JOIN BusinessPlanData ON BusinessPlanMain.PKey = BusinessPlanData.BusinessPlanMainPKey INNER JOIN BusinessPlanDataTemplate ON BusinessPlanDataTemplate.PKey = BusinessPlanData.BusinessPlanDataTemplatePKey INNER JOIN BplText ON BplText.ReferencePKey=BusinessPlanTemplate.PKey INNER JOIN ProductMain ON BusinessPlanData.ProductMainPKey=ProductMain.PKey WHERE SystemPeriod.Year IN ('2016') AND SystemPeriod.PeriodType = 'Year' AND 
SystemPeriod.SalesOrg = 'OrgID1' AND BusinessPlanMain.SalesOrg='OrgID1' AND BusinessPlanData.OrgID ='OrgID1' AND BusinessPlanTemplate.Sheet='Baseline' and ProductMain.MetaType IN ('PrdGroup','Product') 
. 
. 
. 

繼續54個工會

The Result of the above Query is

enter code here 
+1

「我的查詢返回所有54周」 - 一年中有54周? –

+0

感謝米奇小麥的快速回復。請考慮一週中的總週數爲52。 – user2515599

回答

0

你將要使用,而不是所有的UNION子查詢被稱爲UNPIVOT的概念。以下是使用原始查詢的粗略示例 - 您需要研究該主題並更正以下代碼:

WITH OriginalDataSet as (
    [your first query here] 
) 

SELECT ods.Id 
, ods.SalesOrg 
, ods.MetaType 
, ods.[Product ID] 
, ods.Text 
, ods.SystemPeriod 
, BusinessPlanWeek 
, BusinessPlanWeekData 
FROM OriginalDataSet ods 
UNPIVOT (BusinessPlanWeekData FOR BusinessPlanWeek In 
([Week 1], [Week 2], [Week 3], [Week 4], [Week 5], ...)) AS unpvt