新列的子选择总和

时间:2017-11-29 08:52:27

标签: sql oracle

您好我有一个问题,即从子选择中汇​​总两个生成的值。在这种情况下,两列(col1 + col2)的典型总和不起作用,它有什么问题?

 SELECT 
 (
 SELECT COUNT(*)  
 FROM contract c2
 WHERE
 1=1
 AND c2.d_from      <= TO_DATE('31.10.2017', 'DD.MM.YYYY')
 AND c2.d_to         > TO_DATE('31.10.2017', 'DD.MM.YYYY')
 AND c2.d_created   <= TO_DATE('31.10.2017', 'DD.MM.YYYY')
 AND c2.cd_system = 'I'     
 AND c2.cd_product = 'D'
 AND c2.cd_subproduct IN ( '10' ) 
 AND c2.cd_subproduct NOT IN ( '80')
 AND c2.cd_status NOT IN ('09','05')
 ) AS without ,
 (
 SELECT COUNT(*)  
 FROM contract c2
 WHERE
 1=1
 AND c2.d_from      <= TO_DATE('31.10.2017', 'DD.MM.YYYY')
 AND c2.d_to         > TO_DATE('31.10.2017', 'DD.MM.YYYY')
 AND c2.d_created   <= TO_DATE('31.10.2017', 'DD.MM.YYYY')
 AND c2.cd_system = 'I'     
 AND c2.cd_product = 'D'
 AND c2.cd_subproduct IN (( '10' ), ( '80' ))
 AND c2.cd_status NOT IN ('09','05')
 ) AS withs,

 (
 SELECT withs,  without, 
  (
  SELECT COUNT(*)  
  FROM contract c2
  WHERE
  1=1
  AND c2.d_from      <= TO_DATE('31.10.2017', 'DD.MM.YYYY')
  AND c2.d_to         > TO_DATE('31.10.2017', 'DD.MM.YYYY')
  AND c2.d_created   <= TO_DATE('31.10.2017', 'DD.MM.YYYY')
  AND c2.cd_system = 'I'     
  AND c2.cd_product = 'D'
  AND c2.cd_subproduct IN ( '10' ) 
  AND c2.cd_subproduct NOT IN ( '80')
  AND c2.cd_status NOT IN ('09','05')
  ) + 
  (
  SELECT COUNT(*)  
  FROM contract c2
  WHERE
  1=1
  AND c2.d_from      <= TO_DATE('31.10.2017', 'DD.MM.YYYY')
  AND c2.d_to         > TO_DATE('31.10.2017', 'DD.MM.YYYY')
  AND c2.d_created   <= TO_DATE('31.10.2017', 'DD.MM.YYYY')
  AND c2.cd_system = 'I'     
  AND c2.cd_product = 'D'
  AND c2.cd_subproduct IN (( '10' ), ( '80' ))
  AND c2.cd_status NOT IN ('09','05')
  ) as both
 FROM contract c2  
 )
FROM dual
;

每个子选择仅生成一行。我需要总结前两个子选择来获得最终数字,就像在这个表中一样。但是,错误消息警告太多行。提前谢谢你的帮助!

without  withs  both
25        15     50

2 个答案:

答案 0 :(得分:0)

出于测试目的,我使用了双重功能。

SELECT  
(SELECT 1 FROM  dual 
 ) AS without ,
 (
 SELECT 2 FROM dual 
 ) AS withs,

 (

 (
  SELECT 3 FROM dual 
  ) + 
  (
  SELECT 4 FROM  dual 
  ) 

 ) AS both
 FROM dual

它有效。检查一下你的桌子并告诉我。

答案 1 :(得分:0)

也许尝试像

这样的东西
select
 sum(case when
     c2.cd_subproduct IN (( '10' ), ( '80' ))
     AND c2.cd_status NOT IN ('09','05') 
     then 1 else 0
     end)
as without,
sum(case when
    c2.cd_subproduct IN (( '10' ), ( '80' ))
    AND c2.cd_status NOT IN ('09','05')
    then 1 else 0
    end)
as withs,
sum(case when
    AND c2.cd_subproduct IN (( '10' ), ( '80' ))
    AND c2.cd_status NOT IN ('09','05')
    then 1 else 0
    end)
as both
from contract c2
where...

更新将查询置于With语句中,然后使用和不带

求和
with totals as(
 select
  sum(case when
      c2.cd_subproduct IN (( '10' ), ( '80' ))
      AND c2.cd_status NOT IN ('09','05') 
      then 1 else 0
     end) as without,
 sum(case when
     c2.cd_subproduct IN (( '10' ), ( '80' ))
     AND c2.cd_status NOT IN ('09','05')
     then 1 else 0
     end) as withs
 from contract c2
 where...
)

select without, withs, sum(without + withs) as both
from totals
group by without, withs