查询:
SELECT
R.GlobalMarket,
G.AreaName,
SUM(R.Revenue)
FROM REVENUE R
INNER JOIN AREA A
ON R.AnID = A.AnID
GROUP BY R.GlobalMarket, G.AreaName,
给我正确的结果,如:
GlobalMarket Area Revenue
BFS NA Area $20
BFS UK Area $10
Some more values..
我试图将区域结果作为列:
SELECT
R.GlobalMarket,
CASE WHEN G.AreaName = 'NA Area' THEN SUM(R.Revenue) END AS 'NA Revenue',
CASE WHEN G.AreaName = 'UK Area' THEN SUM(R.Revenue) END AS 'UK Revenue',
FROM REVENUE R
INNER JOIN AREA A
ON R.AnID = A.AnID
GROUP BY R.GlobalMarket
它给了我:
GlobalMarket NA Revenue UK Revenue
BFS $20 null
Some more values..
有人能指出,为什么会如此
答案 0 :(得分:2)
嗯,你在CASE
中使用的逻辑似乎不正确。我相信如果您尝试在另一个数据库引擎中运行相同的查询,您可能会收到错误。我认为您必须使用CASE
内的SUM
语句,而不是相反。所以转过来:
CASE WHEN G.AreaName = 'NA Area' THEN SUM(R.Revenue) END AS 'NA Revenue'
进入这个:
SUM(CASE WHEN G.AreaName = 'NA Area' THEN R.Revenue END) AS 'NA Revenue'
请尝试一下,如果有帮助请告诉我。