很難寫,因爲信息你在這裏的代碼,但想法是這樣的:
WITH
cte_A AS
(SELECT qty,product, DATEPART(wk, DATE) AS WEEK
WHERE FORCAST='A'
GROUP BY product,DATEPART(wk, DATE))
, cte_F AS
(SELECT qty,product, DATEPART(wk, DATE) AS WEEK
WHERE FORCAST='F'
GROUP BY product,DATEPART(wk, DATE))
SELECT cte_A.product,cte_A.week,
CASE
WHEN cte_F.qty - cte_A.qty >0 THEN 'Not met forcast'
WHEN cte_F.qty - cte_A.qty <0 THEN 'Met forcast'
END AS result
FROM cte_A join cte_F
ON cte_A.product=cte_F.product
AND cte_A.week=cte_F.week
如果你給我更多的信息,我也許能夠使此代碼更好地爲您。
日期部分功能將會給你一週的人數在一年的任何一天,你就可以使用這個數字每年如果您的數據多年跨越。 希望這有助於。
爲了更好的邏輯(-100〜100)性能,您可以將其更改爲:
WITH
cte_A AS
(SELECT qty,product, DATEPART(wk, DATE) AS WEEK
WHERE FORCAST='A'
GROUP BY product,DATEPART(wk, DATE))
, cte_F AS
(SELECT qty,product, DATEPART(wk, DATE) AS WEEK
WHERE FORCAST='F'
GROUP BY product,DATEPART(wk, DATE))
SELECT cte_A.product,cte_A.week,
CASE WHEN cte_F.qty = cte_A.qty THEN 0
ELSE 100.0*(cte_A.qty - cte_F.qty)/cte_F.qty As Performance
FROM cte_A join cte_F
ON cte_A.product=cte_F.product
AND cte_A.week=cte_F.week
將預期的結果是什麼樣根據您的意見
更新? – GurV
請編輯你的問題你需要哪種輸出? – Darshak
後您預期的結果 – Mansoor