2016-07-06 242 views
3

我正在嘗試添加一個新列來計算當週每個包裝類型的總計百分比。組總數的百分比

以下代碼的結果如下。

image

所以我想新的列中顯示升總數的百分比爲特定的星期。

代碼:

SET DATEFIRST 1 

SELECT 

DATEPART (wk, t0.U_ORC_BE_ProdDate) AS [Week Produced], 
--Display week number of date produced 
(
CASE 
    WHEN t5.U_ORC_BE_NAME LIKE '%Cans%' 
     THEN 'Cans' 
    WHEN t5.U_ORC_BE_NAME LIKE '%Bottles%' 
     THEN 'Bottles' 
    WHEN t5.U_ORC_BE_NAME LIKE '%Key Keg%' 
     THEN 'Key Keg' 
    WHEN t5.U_ORC_BE_NAME LIKE '%SS Keg%' 
     THEN 'SS Keg' 
    WHEN t5.U_ORC_BE_NAME LIKE '%E-Keg%' 
     Then 'SS Keg' 
END 
)AS [Pack Type], 
--Collate item types to pack types 
sum(t5.U_ORC_BE_HECTOLITER * t0.CmpltQty) AS [Total Hectoliters] 
--Calculate total HL in the order 


FROM OWOR T0 
INNER JOIN OITT T1 ON T0.ItemCode = T1.Code 
INNER JOIN OITM T2 ON T1.Code = T2.ItemCode 
LEFT JOIN [@ORC_BE_PACK_TYPE] t5 ON t5.Code = t2.U_ORC_BE_PACK_TYPE 
LEFT JOIN [@ORC_BE_STYLE_H] t6 ON t6.Code= t2.U_ORC_BE_ShortCode 

WHERE 
    (t5.U_ORC_BE_HECTOLITER * t0.CmpltQty) != 0 
AND U_ORC_BE_ProcessType = 'Packaging' 
AND 
    (
    T0.[U_ORC_BE_ProdDate] >= dateadd(wk, datediff(wk, 0, getdate()) - 13, 0) 
    AND T0.[U_ORC_BE_ProdDate] < dateadd(wk, datediff(wk, 0, getdate()), 0) 
    ) 
--For previous 12 weeks 

GROUP BY datepart(wk, t0.U_ORC_BE_ProdDate), 

    t0.U_Operator, 
    (
    CASE 
     WHEN t5.U_ORC_BE_NAME LIKE '%Cans%' 
      THEN 'Cans' 
     WHEN t5.U_ORC_BE_NAME LIKE '%Bottles%' 
      THEN 'Bottles' 
     WHEN t5.U_ORC_BE_NAME LIKE '%Key Keg%' 
      THEN 'Key Keg' 
     WHEN t5.U_ORC_BE_NAME LIKE '%SS Keg%' 
      THEN 'SS Keg' 
     WHEN t5.U_ORC_BE_NAME LIKE '%E-Keg%' 
      Then 'SS Keg' 
    END 
    ) 

ORDER BY datepart (wk, t0.U_ORC_BE_ProdDate) ASC 

回答

1

使用SUM()OVER()聚合全額每個日曆單元和做的百分比計算這一點。

SELECT 
     d.* 
    , ([Total Hectoliters] * 100.0)/SUM([Total Hectoliters]) OVER (PARTITION BY [Week Produced]) [Weekly Pct] 
FROM (
     SELECT 
      DATEPART(wk, t0.U_ORC_BE_ProdDate)  AS [Week Produced] 
      --Display week number of date produced 
      , (
      CASE 
        WHEN t5.U_ORC_BE_NAME LIKE '%Cans%' THEN 'Cans' 
        WHEN t5.U_ORC_BE_NAME LIKE '%Bottles%' THEN 'Bottles' 
        WHEN t5.U_ORC_BE_NAME LIKE '%Key Keg%' THEN 'Key Keg' 
        WHEN t5.U_ORC_BE_NAME LIKE '%SS Keg%' THEN 'SS Keg' 
        WHEN t5.U_ORC_BE_NAME LIKE '%E-Keg%' THEN 'SS Keg' 
      END 
      )           AS [Pack Type] 
      --Collate item types to pack types 
      , SUM(t5.U_ORC_BE_HECTOLITER * t0.CmpltQty) AS [Total Hectoliters] 
     --Calculate total HL in the order 
     FROM OWOR T0 
      INNER JOIN OITT T1 ON T0.ItemCode = T1.Code 
      INNER JOIN OITM T2 ON T1.Code = T2.ItemCode 
      INNER JOIN [@ORC_BE_PACK_TYPE] t5 ON t5.Code = t2.U_ORC_BE_PACK_TYPE 
      LEFT JOIN [@ORC_BE_STYLE_H] t6 ON t6.Code = t2.U_ORC_BE_ShortCode 
     WHERE (t5.U_ORC_BE_HECTOLITER * t0.CmpltQty) <> 0 
      AND U_ORC_BE_ProcessType = 'Packaging' 
      --For previous 12 weeks 
      AND (T0.[U_ORC_BE_ProdDate] >= DATEADD(wk, DATEDIFF(wk, 0, GETDATE()) - 13, 0) 
      AND T0.[U_ORC_BE_ProdDate] < DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0) 
      ) 
     GROUP BY 
      DATEPART(wk, t0.U_ORC_BE_ProdDate) 
      , t0.U_Operator 
      , (
      CASE 
        WHEN t5.U_ORC_BE_NAME LIKE '%Cans%' THEN 'Cans' 
        WHEN t5.U_ORC_BE_NAME LIKE '%Bottles%' THEN 'Bottles' 
        WHEN t5.U_ORC_BE_NAME LIKE '%Key Keg%' THEN 'Key Keg' 
        WHEN t5.U_ORC_BE_NAME LIKE '%SS Keg%' THEN 'SS Keg' 
        WHEN t5.U_ORC_BE_NAME LIKE '%E-Keg%' THEN 'SS Keg' 
      END 
      ) 
) AS d 
ORDER BY 
     [Week Produced] ASC 

順便說一句,因爲你的where子句中堅持認爲,別名T5滿足某些條件下存在使用沒有留下一點上T5加入。也就是說,因爲每一行都必須滿足這些條件,所以t5的NULL結果將被忽略,因此外連接對該表無關緊要。

+0

太棒了。非常感謝。 – coblenski

1

通過查看您的表格截圖,我只寫了這個查詢,而沒有深入到您提供的查詢中。不知道這是你想要的最終結果。如果沒有,您可能需要將其與您的查詢合併:)

SELECT z.WeekProduced, z.PackType, z.packTotal, (z.packTotal/z.WeekTotal) as Percentage 
FROM 
((SELECT WeekProduced, PackType, SUM(Totalhectoliters) as packTotal 
FROM GivenTable 
GROUP BY WeekProduced, PackType) AS x INNER JOIN    
(SELECT WeekProduced, SUM(Totalhectoliters) as WeekTotal 
FROM GivenTable 
GROUP BY WeekProduced) AS y 
ON x.WeekProduced = y.WeekProduced) AS z 
+0

謝謝,這與我正在嘗試的類似,但我不能完全包圍它。 – coblenski