Solved

PL/SQL Two PAYER_IDs with PAYER_TYPES these two types

Posted on 2016-10-18
4
42 Views
Last Modified: 2016-10-18
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
0
Comment
Question by:9apit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 41848988
You didn't provide any sample data or expected results so I dummied up some test data that I think is what you are after.

Take a look at the select below.  If it isn't correct, please add to the test case and explain where it went wrong.

/*
--drop table tab1 purge;
create table tab1(payer_ids 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;
*/

select payer_ids from (
	select payer_ids,
		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_ids
)
where total_count=2 and has_17001=1 and has_17002=1
/

Open in new window

0
 

Author Closing Comment

by:9apit
ID: 41849006
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
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 41849022
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

0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 41849026
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

0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question