在db2中组合两个select语句的结果

t0ybt7op  于 2022-11-07  发布在  DB2
关注(0)|答案(1)|浏览(214)

我是SQL的新手,我使用的是db2。我有下面两个单独的查询,它们各自返回预期的结果。但是,我希望将查询2的结果除以查询1的结果。
查询1:

select count(*) total
      from ltl400tst3.frp001
      JOIN ETLLIB.CYMD_TO_DATE
          ON fhpdat = cymd_date
      left join ltl400mod3.pup092
          on pcpro = fhpro
      left join LTL400MOD3.PUP090
          on PCNUM = PANUM and PCTID = PATID
      left join ltl400mod3.pup300 on pctid = cotid and pcnum = conum and pcline = coline and fhccd = coccd
      WHERE fhbtc in ('6A16579', '6R16579') and PASTAT != 'C' and DATE = '2021-09-01';

疑问二:

select Date, case cqtype when 'O' then 'Override' when 'S' then 'Alert' else 'Other' end record_type, count(*) total
      from LTL400MOD3.PUP301
      left join LTL400MOD3.PUP300
          on COTID = CQTID and CONUM = CQNUM and COLINE = CQLINE
      join ETLLIB.CYMD_TO_DATE
      on (CAST(CQRSSTMP AS DATE)) = DATE
      left join MASTER.ACCOUNT_HIERARCHY
          on COCCD = ACCOUNT
      where ((CQSACIID = '17') or (TOP_LEVEL_ACCOUNT = '0P16579')) and DATE = '2021-09-01'
group by date, CQTYPE;

我的第一个想法是使用一个CTE,但我在格式化表达式以从多个CTE执行函数时遇到了一些困难。提前感谢您的建议。

olqngx59

olqngx591#

我相信有更好的方法来实现您的目标,但是由于您没有提供示例数据、业务逻辑或预期的输出,我将尝试回答您的确切问题,而不是尝试重构您的查询。

with 
 query_1 (total) as (your_query_1_select_statement)

,query_2 (record_type, total) as  (your_query_2_select_statement)

select record_type, total/(select total from query_1) as ratio
from query_2;

相关问题