Link to home
Start Free TrialLog in
Avatar of Allen Pitts
Allen PittsFlag for United States of America

asked on

PL/SQL Two PAYER_IDs with PAYER_TYPES these two types

Hello expert,

Payers have PAYER_IDS and PAYER_TYPE_CODES.
Looking for Payers that have two PAYER_TYPE_CODES
where those two PAYER_TYPES_CODES are 17001 and 17002.

Tried the query copied herewith below.
Returns all PAYER_IDS w PAYER_TYPE__CODES 17002
not just the PAYER_IDS w PAYER_TYPE__CODES 17002
and PAYER_TYPE__CODES 17001

Tried the query copied below with AND instead of OR
but this returned no rows.

Also tried WHERE IN (17001, 17002) Returns similar to OR.

I think I have done this using paretheses  but can't remember.

Thanks.

Allen Pitts, Dallas Texas

With cte as (
select
PAyer_ID
FROM
CCM_PAYER_TYPE
group by PAyer_ID
having Count(*) =2
order by PAYER_ID)
SELECT PAYER_TYPE_ID,
  CTE.PAYER_ID,
  PAYER_TYPE_CODE
FROM CTE
join CCM_PAYER_TYPE PAT on PAT.payer_id =CTE.payer_id
where (PAYER_TYPE_CODE = 17001 or
 PAYER_TYPE_CODE = 17002)
order by 2, 3
ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Allen Pitts

ASKER

Thanks. Works. Tested solution:

  with CTE as
(
select payer_id from (
      select payer_id,
            count(case when payer_type_code = 17001 then 1 end) has_17001,
            count(case when payer_type_code = 17002 then 1 end) has_17002,
            count(*) total_count
      from CCM_PAYER_TYPE
      group by payer_id
)
where total_count=2 and has_17001=1 and has_17002=1
)
SELECT
CTE.PAYER_ID,
PAYER_TYPE_CODE
from CTE
join CCM_PAYER_TYPE PAT on CTE.PAYER_ID = PAT.PAYER_ID
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

CTE's are nice when you need them but you really don't need it here.

Here is a revised test case with two tables that pulls data from both:
/*
drop table tab1 purge;
create table tab1(payer_id number, payer_type_codes number);

insert into tab1 values(1,17001);
insert into tab1 values(1,17002);
insert into tab1 values(2,17001);
insert into tab1 values(2,17003);
insert into tab1 values(3,17001);
insert into tab1 values(3,17001);
insert into tab1 values(4,17001);
commit;

drop table tab2 purge;
create table tab2(payer_id number, some_other_column char(1));
insert into tab2 values(1,'A');
insert into tab2 values(2,'B');
commit;
*/

select t1.payer_id, t2.some_other_column from (
	select payer_id,
		count(case when payer_type_codes = 17001 then 1 end) has_17001,
		count(case when payer_type_codes = 17002 then 1 end) has_17002,
		count(*) total_count
	from tab1
	group by payer_id
) t1
join tab2 t2 on t1.payer_id=t2.payer_id
where total_count=2 and has_17001=1 and has_17002=1
/

Open in new window

You can even move the join inside the outer query but you need to join on all the columns you want.  You would need to test them to see which performs better:

select payer_id, some_other_column from (
	select t1.payer_id, t2.some_other_column,
		count(case when payer_type_codes = 17001 then 1 end) has_17001,
		count(case when payer_type_codes = 17002 then 1 end) has_17002,
		count(*) total_count
	from tab1 t1 join tab2 t2 on t1.payer_id=t2.payer_id
	group by t1.payer_id, some_other_column
) t1
where total_count=2 and has_17001=1 and has_17002=1
/

Open in new window