0
我有兩個oracle(11g)SQL查詢,每個查詢都給出一個數字結果。什麼是從兩個SQL查詢的數字結果計算百分比的更好方法
SQL1:
select count(*) as count1
from table_name1
where col1_number in (2400)
and (col2_varchar2 = '' or col2_varchar2 is null or col2_varchar2 = 'BLANK' or
col2_varchar2 = 'TBA' or col2_varchar2 like 'B%')
and col3_varchar2 = 'app31'
and col4_date > (sysdate - 7)
SQL2:
select count(*) as count2
from table_name1
where col1_number in (2400)
and col3_varchar2 = 'app31'
and col4_date > (sysdate - 7)
可以說,我得到35從SQL1結果和831從SQL2結果。然後我想編寫一個SQL,它將從SQL1和SQL2中獲取這兩個結果並計算(35 * 100)/ 831。
我已經寫了一個SQL來做到這一點。
SQL3:
select round((count1 * 100)/count2,3) as percent_missing from
(
with temp_table1 as (
select count(*) as count1
from table_name1
where col1_number in (2400)
and (col2_varchar2 = '' or col2_varchar2 is null or col2_varchar2 =
'BLANK' or col2_varchar2 = 'TBA' or col2_varchar2 like 'B%')
and col3_varchar2 = 'app31'
and col4_date > (sysdate - 7)
),
temp_table2 as (
select count(*) as count2
from table_name1
where col1_number in (2400)
and col3_varchar2 = 'app31'
and col4_date > (sysdate - 7)
)
select count1, count2
from temp_table1, temp_table2
);
問:有沒有什麼更好的方法來做到這一點?我想知道在這種情況下是否有其他更好的方法。
感謝您的回覆,刪除重複的「and」和「as」後,您建議的SQL與SQL3的工作方式相同。它看起來更乾淨,所以我打算使用這個版本。 – 300