Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

combine query to fetch count with 1 query

Md Shahrukh AliNov 19 2024

Hi,
I am new to Oracle DBA.
I came across query where I have 3 tables to connect and get data.

Table1 master_intr => column - id, p_id, dob, ssn
Table2 rel_master_intr => a_id, p_id, rel, flag
Table3 statistics_intr => column - ref_no

Based on the table2 rel_master_intr column => rel and flag we need to fetch data from master_intr
I have written below query to do the same.
Is there a better way to do it.
As query is same the only diff is rel and flag can we have some group by clause as i need the count

select count(*) from master_intr where p_id in
(select distinct(p_id) from rel_master_intr where rel='SOW' and flag='Y' and a_id in 
(select ref_no from statistics_intr));

select count(*) from master_intr where p_id in
(select distinct(p_id) from rel_master_intr where rel='SOW' and flag='Y' and a_id in 
(select ref_no from statistics_intr))
and dob is not null;

select count(*) from master_intr where p_id in
(select distinct(p_id) from rel_master_intr where rel='SOW' and flag='Y' and a_id in 
(select ref_no from statistics_intr))
and ssn is not null;
select count(*) from master_intr where p_id in
(select distinct(p_id) from rel_master_intr where rel='JFA' and flag='N' and a_id in 
(select ref_no from statistics_intr));

select count(*) from master_intr where p_id in
(select distinct(p_id) from rel_master_intr where rel='JFA' and flag='N' and a_id in 
(select ref_no from statistics_intr))
and dob is not null;

select count(*) from master_intr where p_id in
(select distinct(p_id) from rel_master_intr where rel='JFA' and flag='N' and a_id in 
(select ref_no from statistics_intr))
and ssn is not null;
select count(*) from master_intr where p_id in
(select distinct(p_id) from rel_master_intr where rel='JFO' and flag='N' and a_id in 
(select ref_no from statistics_intr));

select count(*) from master_intr where p_id in
(select distinct(p_id) from rel_master_intr where rel='JFO' and flag='N' and a_id in 
(select ref_no from statistics_intr))
and dob is not null;

select count(*) from master_intr where p_id in
(select distinct(p_id) from rel_master_intr where rel='JFO' and flag='N' and a_id in 
(select ref_no from statistics_intr))
and ssn is not null;
Comments
Post Details
Added on Nov 19 2024
3 comments
266 views