我有以下查询效果很好,但我还想确定每个产品(名称)的收入,这些产品的价格根据配送中心的不同而不同:
SELECT NAME,
ISNULL([UK - BOLTON], 0) [UK Website],
ISNULL([USA - FLORIDA], 0) [USA Website],
ISNULL([CANADA], 0) [Canada Website],
ISNULL([AUSTRALIA], 0) [Australia Website],
ISNULL([AMAZON (USA)], 0) [Amazon (USA)],
ISNULL([AMAZON (UK)], 0) [Amazon (UK)],
ISNULL([AMAZON (FR)], 0) [Amazon (FR)],
ISNULL([AMAZON (CA)], 0) [Amazon (CA)],
ISNULL([AMAZON (JP)], 0) [Amazon (JP)]
FROM (SELECT p.NAME,
ps.UNITSSOLD,
CASE
WHEN dc.[DESCRIPTION] = 'UK Website' THEN p.UKWEBPRICE
END Revenue,
dc.[DESCRIPTION]
FROM DBO.PRODUCTSALES ps
INNER JOIN DBO.PRODUCT p
ON ps.PRODUCTID = p.PRODUCTID
INNER JOIN DBO.DISTRIBUTIONCENTRE dc
ON ps.DISTRIBUTIONCENTREID = dc.DISTRIBUTIONCENTREID
WHERE ps.ORDERDATE >= CONVERT(DATETIME, '2013-08-01 00:00:00', 102)
AND ps.ORDERDATE <= CONVERT(DATETIME, '2013-08-20 00:00:00', 102)
AND p.PRODUCTCLASSIFICATIONID = 2) [SourceTable]
PIVOT ( SUM(UNITSSOLD)
FOR [DESCRIPTION] IN ([UK - Bolton],
[USA - Florida],
[Canada],
[Australia],
[Amazon (USA)],
[Amazon (UK)],
[Amazon (FR)],
[Amazon (CA)],
[Amazon (JP)]) ) AS PIVOTTABLE
如您所见,我尝试过使用 CASE/WHEN,但我想我对如何做到这一点感到困惑。基本上,英国 - 博尔顿应该从每个配送中心的 Product 表 (p) 等中获取 UKWebPRice,它是收入,所以每个中心应该是 UnitsSold * UKWebPrice 等。
希望这是有道理的。
谢谢,
麦克风