Solved

PL/SQL Two PAYER_IDs with PAYER_TYPES these two types

Posted on 2016-10-18
4
33 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
  • 3
4 Comments
 
LVL 76

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 76

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 76

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

948 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now