proc SQL;
CREATE TABLE DATA.DUMMY AS
SELECT *,
CASE
WHEN (Discount IS NOT NULL)
THEN (Total_Retail_Price - (Total_Retail_Price * Discount)) * Quantity AS Rev
ELSE (Total_Retail_Price * Quantity) AS Rev
END
FROM DATA.Cumulative_Profit_2013 AS P
;
我试图将潜在的NULL列作为Revenue表达式的一部分。但我的案例陈述引发了一些问题。我已经检查了其他例子,但我看不出为什么会有所帮助
答案 0 :(得分:4)
看起来您可以使用COALESCE
来实现您的目标,而无需明确的条件:
SELECT *,
(Total_Retail_Price - (Total_Retail_Price * COALESCE(Discount, 0))) * Quantity AS Rev
FROM DATA.Cumulative_Profit_2013 AS P
答案 1 :(得分:3)
不知道SAS,正常的SQL语法是:
SELECT *,
CASE
WHEN (Discount IS NOT NULL)
THEN (Total_Retail_Price - (Total_Retail_Price * Discount)) * Quantity
ELSE (Total_Retail_Price * Quantity)
END AS Rev
即,在case表达式结束后使用列别名。
答案 2 :(得分:1)
作为一个说明,我会把它写成:
SELECT p.*,
(Total_Retail_Price * (1 - COALESCE(Discount, 0)) * Quantity) AS Rev
FROM DATA.Cumulative_Profit_2013 AP
我发现原始表达式中Total_Retail_Price
的重复更难以遵循。
注意:所有答案都适用于SAS。