我正在Teradata內部使用兩個表格,我試圖按商店編號,總銷售天數和每個商店編號中的總天數查詢每個活動項目。我當前的查詢設置是通過SKU提供兩行數據,而不是提供顯示適當信息的一行。JOIN和CASE重複查詢結果
表1:顯示班級編號,班級名稱,狀態和存儲數據。
Store Item Class Number Class Name Status
100 Apple 10 Red Fruit Active
200 Apple 10 Red Fruit Active
100 Banana 12 Yellow Fruit Active
200 Banana 12 Yellow Fruit Active
100 Pear 14 Green Fruit Active
200 Pear 14 Green Fruit Active
100 Beans 20 Green Vegetable Discontinued
200 Beans 20 Green Vegetable Active
表2:顯示總天店和項目出售
Store Item Total Days to sell
100 Apple 4
200 Apple 1
100 Banana 2
200 Banana 4
100 Pear 3
200 Pear 6
100 Beans NULL
200 Beans 4
表3:當前查詢結果
Item Class Number Class Name Total Days to sell Store 100 Store 200
Apple 10 Red Fruit 5 4 NULL
Apple 10 Red Fruit 5 NULL 1
Banana 12 Yellow Fruit 6 2 NULL
Banana 12 Yellow Fruit 6 NULL 4
Pear 14 Green Fruit 9 3 NULL
Pear 14 Green Fruit 9 NULL 6
Beans 20 Green Vegetable 4 NULL 4
下面是如何我找的數據是舉辦:
Item Class Number Class Name Total Days to sell Store 100 Store 200
Apple 10 Red Fruit 5 4 1
Banana 12 Yellow Fruit 6 2 4
Pear 14 Green Fruit 9 3 6
Beans 20 Green Vegetable 4 NULL 4
Cur租金查詢:
SELECT DISTINCT
A.ITEM,
A.CLASS_NUMBER,
A.CLASS_NAME,
SUM(B.TOTAL_DAYS_TO_SELL),
CASE
WHEN B.STORE=100 THEN B.TOTAL_DAYS_TO_SELL
ELSE NULL
END AS STORE 100,
CASE
WHEN B.STORE=200 THEN B.TOTAL_DAYS_TO_SELL
ELSE NULL
END AS STORE 200
FROM TABLE 1 A
RIGHT JOIN TABLE 2 B
ON B.ITEM=A.ITEM
WHERE A.STATUS='ACTIVE'
GROUP BY
A.ITEM,
A.CLASS_NUMBER,
A.CLASS_NAME,
STORE 100,
STORE 200
ORDER BY
A.CLASS_NUMBER ASC;
請讓我知道如果您有任何有關的信息
感謝任何問題!
在最後兩個case語句周圍貼上一個'Sum()',並將它們從您的組中取出。 – JNevill
case語句附近的函數修復了重複的行,但銷售總天數現在翻倍了。這是可以修復的嗎? – Hery0502