我是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执行函数时遇到了一些困难。提前感谢您的建议。
1条答案
按热度按时间olqngx591#
我相信有更好的方法来实现您的目标,但是由于您没有提供示例数据、业务逻辑或预期的输出,我将尝试回答您的确切问题,而不是尝试重构您的查询。