2

代码的第一部分基于 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

但是,我还有两个其他代码部分要合并到上面的代码中,但它们有一个额外的“位置”标准。

所以这些是我想结合到上表中的两个 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_balrec_bal在一个表中,第一个表包含FOPS_REHABFOPS_RECOVERY。我遇到了问题,因为他们有一个额外的子句(where fops recovery = 1 or fops rehab = 1)。

4

1 回答 1

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
于 2012-09-14T11:38:02.957 回答