I'm still not sure I fully understand what you're trying to do. But the solution is similar to that for your other question.
First you need to outer join all the tables together. Then you can find whether all the FK_TB_CCRS_PAT_REQUEST columns are null for a given company and master with analytics. For example, min partitioned by these columns:
min(t.FK_TB_CCRS_PAT_REQUEST) over (
partition by t.fk_tb_login_master, t.fk_tb_company_details
)
You can do similar with count to check the number of rec_statuses = 'AC'. But here you want to map the other statuses to null in the count.
count(case when a.rec_status = 'AC' then 1 end) over (
partition by t.fk_tb_login_master, t.fk_tb_company_details
)
Bung these in case expressions as appropriate and you get:
select t.fk_tb_login_master, t.fk_tb_company_details,
min(t.FK_TB_CCRS_PAT_REQUEST) over (
partition by t.fk_tb_login_master, t.fk_tb_company_details
) mn,
count(case when a.rec_status = 'AC' then 1 end) over (
partition by t.fk_tb_login_master, t.fk_tb_company_details
) ct,
case
when min(t.FK_TB_CCRS_PAT_REQUEST) over (
partition by t.fk_tb_login_master, t.fk_tb_company_details
) is null then
case
when count(case when a.rec_status = 'AC' then 1 end) over (
partition by t.fk_tb_login_master, t.fk_tb_company_details
) > 0 then 'Y'
else 'N'
end
else
case
when count(case when r.rec_status = 'AC' then 1 end) over (
partition by t.fk_tb_login_master, t.fk_tb_company_details
) > 0 then 'Y'
else 'N'
end
end yn
from tab_login_details t
left join tab_access a
on t.PK_TB_CCRS_LOGIN_DETAILS = a.FK_TB_CCRS_LOGIN_DETAILS
left join tab_request r
on r.PK_TB_CCRS_PAT_REQUEST = t.FK_TB_CCRS_PAT_REQUEST
order by 1 , 2;
FK_TB_LOGIN_MASTER FK_TB_COMPANY_DETAILS MN CT Y
------------------ --------------------- ---------- ---------- -
1022 1001 1 Y
1022 1001 1 Y
1022 1001 1 Y
1022 1001 1 Y
1022 1002 0 N
1022 1002 0 N
1022 1003 1111 0 Y
1022 1003 1111 0 Y
1022 1003 1111 0 Y
1022 1003 1111 0 Y
1022 1003 1111 0 Y
Which squashes down to:
select distinct t.fk_tb_login_master, t.fk_tb_company_details,
case
when min(t.FK_TB_CCRS_PAT_REQUEST) over (
partition by t.fk_tb_login_master, t.fk_tb_company_details
) is null then
case
when count(case when a.rec_status = 'AC' then 1 end) over (
partition by t.fk_tb_login_master, t.fk_tb_company_details
) > 0 then 'Y'
else 'N'
end
else
case
when count(case when r.rec_status = 'AC' then 1 end) over (
partition by t.fk_tb_login_master, t.fk_tb_company_details
) > 0 then 'Y'
else 'N'
end
end yn
from tab_login_details t
left join tab_access a
on t.PK_TB_CCRS_LOGIN_DETAILS = a.FK_TB_CCRS_LOGIN_DETAILS
left join tab_request r
on r.PK_TB_CCRS_PAT_REQUEST = t.FK_TB_CCRS_PAT_REQUEST
order by 1 , 2;
FK_TB_LOGIN_MASTER FK_TB_COMPANY_DETAILS Y
------------------ --------------------- -
1022 1001 Y
1022 1002 N
1022 1003 Y