Solved

PL/SQL Two PAYER_IDs with PAYER_TYPES these two types

Posted on 2016-10-18
4
26 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

705 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

19 Experts available now in Live!

Get 1:1 Help Now