所以我有一个名为TOY_DETAILS的表,包括ID,Date,Toy_obje,货币(可以是美元,英镑或欧元)和预算。
现在我想创建一个将返回所有内容的SQL查询
我尝试了什么。
SELECT ID, Date,Toy_object, currency, budget,
CASE currency
WHEN 'GBP' THEN budget*1.5
WHEN 'EUR' THEN budget*1.2
ELSE budget
END AS USD_Value
(SELECT SUM(USD_Value) FROM PROJECT_DETAILS) AS Total_Budget
FROM PROJECT_DETAILS
我的问题是2)。我希望Total_Budget成为USD_Value列的SUM,但我无法输入' SUM(USD_Value)'因为该列不存在' ?
提前致谢,
答案 0 :(得分:2)
您必须在CASE
:
SUM
表达式
SELECT ID, Date,Toy_object, currency, budget,
CASE currency
WHEN 'GBP' THEN budget*1.67
WHEN 'EUR' THEN budget*1.3
ELSE budget END AS USD_Value,
SUM(CASE currency
WHEN 'GBP' THEN budget*1.67
WHEN 'EUR' THEN budget*1.3
ELSE budget
END) OVER() AS Total_Budget
FROM PROJECT_DETAILS