减去子查询计数DB2

时间:2017-06-28 06:13:14

标签: db2

我正在尝试区分2个子查询的计数。但我得到以下错误。我分别执行了两个子查询,它工作正常。该错误可能是因为我尝试将它们转换为单个查询。有什么想法吗?

enter image description here

select
(select count(*) FROM TESTEA.AIT_P_NUMBER where 
rtrim(ltrim(translate(PNU_POL,' ','1234567890'))) = '') 
- 
(select count(*) FROM TESTEA.AIT_P_NUMBER )

5 个答案:

答案 0 :(得分:1)

您可以尝试从DUAL中选择:

SELECT
    (SELECT COUNT(*) FROM TESTEA.AIT_P_NUMBER
     WHERE RTRIM(LTRIM(TRANSLATE(PNU_POL, ' ', '1234567890'))) = '') -
    (SELECT COUNT(*) FROM TESTEA.AIT_P_NUMBER)
FROM DUAL;

实际上,您可以使用涉及条件​​聚合的单一查询解决方法:

SELECT
    SUM(CASE WHEN RTRIM(LTRIM(TRANSLATE(PNU_POL, ' ', '1234567890'))) = ''
             THEN 1 ELSE 0 END) - COUNT(*)
FROM TESTEA.AIT_P_NUMBER

答案 1 :(得分:1)

解决方案1:

values  (SELECT COUNT(*) FROM TESTEA.AIT_P_NUMBER where trim(translate(PNU_POL,'          ','1234567890')) = '') 
      - (SELECT COUNT(*) nb FROM TESTEA.AIT_P_NUMBER)

答案 2 :(得分:0)

解决方案2:

SELECT sum(case when trim(translate(PNU_POL,'           ','1234567890')) = '' then 1 else 0 end) - count(*)
FROM TESTEA.AIT_P_NUMBER

答案 3 :(得分:0)

解决方案3:

select  (SELECT COUNT(*) FROM TESTEA.AIT_P_NUMBER where trim(translate(PNU_POL,' ','1234567890')) = '') 
      - (SELECT COUNT(*) nb FROM TESTEA.AIT_P_NUMBER)
from SYSIBM.SYSDUMMY1

答案 4 :(得分:0)

解决方案4:

select sum(nb) from 
(                                    
  select count(*) nb                                       
  from TESTEA.AIT_P_NUMBER where trim(translate(PNU_POL,'          ','1234567890')) = ''     
  union all                                                
  select  -count(*) nb                                     
  from TESTEA.AIT_P_NUMBER                                            
) nb