我有2个以下的选择查询。我想将第一个选择查询结果的结果与第二个选择查询结果相除,并将此结果乘以100以计算百分比。但不知道该怎么做。
select count(*) from rate_errors where id > to_char(SYSTIMESTAMP - INTERVAL '1' HOUR,'YYYYMMDDHH24MISS')||'0000'
select count(*) from SDR_O2 where id > to_char(SYSTIMESTAMP - INTERVAL '1' HOUR,'YYYYMMDDHH24MISS')||'0000'
答案 0 :(得分:5)
我倾向于用cross join
:
select r.cnt / o.cnt
from (select count(*) as cnt
from rate_errors
where id > to_char(SYSTIMESTAMP - INTERVAL '1' HOUR, 'YYYYMMDDHH24MISS') ||'0000'
) r cross join
(select count(*) as cnt
from SDR_O2
where id > to_char(SYSTIMESTAMP - INTERVAL '1' HOUR, 'YYYYMMDDHH24MISS')||'0000'
) o;
如果需要,可以将id
计算分解为CTE:
with params as (
select to_char(SYSTIMESTAMP - INTERVAL '1' HOUR, 'YYYYMMDDHH24MISS')||'0000'
from dual
)
select r.cnt / o.cnt
from (select count(*) as cnt
from params cross join rate_errors re
where re.id > params.id
) r cross join
(select count(*) as cnt
from params cross join SDR_O2 o
where o.id > params.id
) o;
这样可以更轻松地更改id
,并确保两个子查询的逻辑相同。
答案 1 :(得分:1)
with x as
(select count(*) rcnt from rate_errors where id > to_char(SYSTIMESTAMP - INTERVAL '1' HOUR,'YYYYMMDDHH24MISS')||'0000')
, y as (select count(*) scnt from SDR_O2 where id > to_char(SYSTIMESTAMP - INTERVAL '1' HOUR,'YYYYMMDDHH24MISS')||'0000')
select 100*rcnt/scnt from x, y;
您可以使用cte&#39>来完成此操作。但是,如果上面的x和y之间没有关系,则无法确定结果。