代码的第一部分基于SUM语句创建表:
SELECT varday, COUNT(*) AS Vol,
SUM(FOPS_REHAB) AS FOPS_REHAB,
SUM(FOPS_RECOVERY) AS FOPS_RECOVERY
FROM table
WHERE PROCESSING_DATE > '31/aug/2012' and rownumber =1
GROUP BY varday
ORDER BY varday
但是,我还有两个其他代码部分,我想将它们合并到上面的代码中,但是它们有一个额外的“where”标准。
所以这些是我想要结合到上表中的两个sum语句;
select SUM(Current_Balance_Amount)as rec_bal, varday
into #temp
from table
where fops_recovery = 1 and PROCESSING_DATE > '31/aug/2012' and rownumber =1
group by varday
ORDER BY varday
select SUM(Current_Balance_Amount)as reh_bal, varday
into #temp
from table
where fops_rehab = 1 and PROCESSING_DATE > '31/aug/2012' and rownumber =1
group by varday
ORDER BY varday
所以reh_bal
和rec_bal
位于一个表格中,第一个表格包含FOPS_REHAB
和FOPS_RECOVERY
。我遇到了问题,因为他们有一个额外的条款(where fops recovery = 1 or fops rehab = 1
)。
答案 0 :(得分:0)
我相信所有3个查询都可以组合成一个,试试这个:
select varday,
COUNT(*) AS Vol,
SUM(FOPS_REHAB) AS FOPS_REHAB,
SUM(FOPS_RECOVERY) AS FOPS_RECOVERY,
SUM(CASE WHEN fops_recovery = 1 THEN Current_Balance_Amount ELSE 0 END) rec_bal,
SUM(CASE WHEN fops_rehab = 0 THEN Current_Balance_Amount ELSE 0 END) reh_bal
where PROCESSING_DATE > '31/aug/2012' and rownumber =1
group by varday
ORDER BY varday