我正在使用以下代码,并希望从先前CASE中的SUM中提取列Total_Bank_Amount
和Prog_Variance
。
SELECT
b.Date,
SUM(p.Prog_Amount) AS Prog_Amount,
SUM(o.Credit_Amt) AS Outstanding,
CASE
WHEN b.Bank_Name Like '%Corp%'
THEN SUM(b.Credit_Amt)
END AS TMP_Amount,
CASE
WHEN b.Bank_Name Like '%Cleo%'
THEN SUM(b.Credit_Amt)
END AS Cleo_Amount,
CASE
WHEN b.Bank_Name Like '%NY Bank%'
THEN SUM(b.Credit_Amt)
END AS NY_Amount,
CASE
WHEN b.Bank_Name Like '%MA%'
THEN SUM(b.Credit_Amt)
END AS MA_Amount,
CASE
WHEN b.Bank_Name Like '%CT%'
THEN SUM(b.Credit_Amt)
END AS CT_Amount,
CASE
WHEN b.Bank_Name Like '%NY_SS%'
THEN SUM(b.Credit_Amt)
END AS NY_SS_Amount,
CASE
WHEN b.Bank_Name LIKE '%VC_SS%'
THEN SUM(b.Credit_Amt)
END AS VC_SS_Amount,
(TMP_Amount + Cleo_Amount + NY_Amount + MA_Amount + CT_Amount + NY_SS_Amount + VC_SS_Amount) AS Total_Bank_Amount,
(Prog_Amount + Outstanding - TMP_Amount - Cleo_Amount - NY_Amount - MA_Amount - CT_Amount - NY_SS_Amount - VC_SS_Amount) AS Prog_Variance
FROM
vw_Prog_Reference_Summary p
RIGHT JOIN
Bank_Detail b ON p.Bank_Reference_Number = b.Bank_Reference
LEFT JOIN
vw_Outstanding_Form o ON b.Bank_Reference = o.Bank_Reference
LEFT JOIN
Exclusion e ON b.Bank_Reference = e.Exclude
WHERE
e.Exclude IS NULL
GROUP BY
b.Bank_Name, b.Date
问题是这些不是在他们解析时创建的,所以你如何解决这个问题?研究让我想出了什么=(。谢谢。
答案 0 :(得分:1)
很遗憾,您无法在同一个select语句中引用列别名。通常,解决此问题的方法是使您的核心查询成为子查询,然后引用列别名,或者您可以在计算中再次重复相同的逻辑(因此您将再次在聚合计算中重复所有这些case和sum语句) 。
以下链接供参考,因为之前已经提出过: this commit
答案 1 :(得分:0)
尝试这样的事情:
SELECT
(TMP_Amount + Cleo_Amount + NY_Amount + MA_Amount + CT_Amount + NY_SS_Amount + VC_SS_Amount) AS Total_Bank_Amount,
(Prog_Amount + Outstanding - TMP_Amount - Cleo_Amount - NY_Amount - MA_Amount - CT_Amount - NY_SS_Amount - VC_SS_Amount) AS Prog_Variance
FROM (
SELECT b.Date, Sum(p.Prog_Amount) AS Prog_Amount, Sum(o.Credit_Amt) AS Outstanding,
CASE WHEN b.Bank_Name Like '%Corp%' THEN SUM(b.Credit_Amt) END AS TMP_Amount,
CASE WHEN b.Bank_Name Like '%Cleo%' THEN SUM(b.Credit_Amt) END AS Cleo_Amount,
CASE WHEN b.Bank_Name Like '%NY Bank%' THEN SUM(b.Credit_Amt)END AS NY_Amount,
CASE WHEN b.Bank_Name Like '%MA%' THEN SUM(b.Credit_Amt) END AS MA_Amount,
CASE WHEN b.Bank_Name Like '%CT%' THEN SUM(b.Credit_Amt) END AS CT_Amount,
CASE WHEN b.Bank_Name Like '%NY_SS%' THEN SUM(b.Credit_Amt)END AS NY_SS_Amount,
CASE WHEN b.Bank_Name Like '%VC_SS%' THEN SUM(b.Credit_Amt)END AS VC_SS_Amount
FROM vw_Prog_Reference_Summary p
RIGHT JOIN Bank_Detail b ON p.Bank_Reference_Number = b.Bank_Reference
LEFT JOIN vw_Outstanding_Form o ON b.Bank_Reference = o.Bank_Reference
LEFT JOIN Exclusion e ON b.Bank_Reference = e.Exclude
WHERE e.Exclude Is Null
GROUP BY b.Bank_Name, b.Date
) t
我将旧查询移动到子查询,以便您可以使用所需的值。我还移动了Total_Bank_Amount
和Prog_Variance
之外的摘要。
答案 2 :(得分:0)
我认为您正在寻找条件聚合:
SELECT b.Date, Sum(p.Prog_Amount) AS Prog_Amount, Sum(o.Credit_Amt) AS Outstanding,
SUM(CASE WHEN b.Bank_Name Like '%Corp%' THEN b.Credit_Amt END) AS TMP_Amount,
. . .
FROM . . .
GROUP BY b.Date;
也就是说,您希望CASE
作为SUM()
的参数,而不是相反。
这(合理地)假设您实际上并不想为每个银行分别设置一个行,而只需要汇总的信息。
答案 3 :(得分:0)
阅读Rollup, Cube and Grouping Sets。这将为您提供相同选择中的总和(如果这是您想要的),但您必须考虑NULL值。
或者,您可以UNION
使用WITH
,如下所示:
With SummaryData As
(
SELECT
b.Date, Sum(p.Prog_Amount) AS Prog_Amount, Sum(o.Credit_Amt) AS Outstanding,
CASE WHEN b.Bank_Name Like '%Corp%' THEN SUM(b.Credit_Amt) END AS TMP_Amount,
CASE WHEN b.Bank_Name Like '%Cleo%' THEN SUM(b.Credit_Amt) END AS Cleo_Amount,
CASE WHEN b.Bank_Name Like '%NY Bank%' THEN SUM(b.Credit_Amt)END AS NY_Amount,
CASE WHEN b.Bank_Name Like '%MA%' THEN SUM(b.Credit_Amt) END AS MA_Amount,
CASE WHEN b.Bank_Name Like '%CT%' THEN SUM(b.Credit_Amt) END AS CT_Amount,
CASE WHEN b.Bank_Name Like '%NY_SS%' THEN SUM(b.Credit_Amt)END AS NY_SS_Amount,
CASE WHEN b.Bank_Name Like '%VC_SS%' THEN SUM(b.Credit_Amt)END AS VC_SS_Amount,
(TMP_Amount + Cleo_Amount + NY_Amount + MA_Amount + CT_Amount + NY_SS_Amount + VC_SS_Amount) AS Total_Bank_Amount,
(Prog_Amount + Outstanding - TMP_Amount - Cleo_Amount - NY_Amount - MA_Amount - CT_Amount - NY_SS_Amount - VC_SS_Amount) AS Prog_Variance
FROM vw_Prog_Reference_Summary p
RIGHT JOIN Bank_Detail b
ON p.Bank_Reference_Number = b.Bank_Reference
LEFT JOIN vw_Outstanding_Form o
ON b.Bank_Reference = o.Bank_Reference
LEFT JOIN Exclusion e
ON b.Bank_Reference = e.Exclude
WHERE e.Exclude Is Null
GROUP BY b.Bank_Name, b.Date
)
Select *
From SummaryData
Union All
SELECT
Date,
0 Prog_Amount,
0 Outstanding,
0 TMP_Amount,
0 Cleo_Amount,
0 NY_Amount,
0 MA_Amount,
0 CT_Amount,
0 NY_SS_Amount,
0 VC_SS_Amount,
Sum (Total_Bank_Amount) Total_Bank_Amount,
Sum (Prog_Variance) Prog_Variance
FROM SummaryData
Group By
Date