Solved

PL/SQL Two PAYER_IDs with PAYER_TYPES these two types

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

Veeam gives away 10 full conference passes

Veeam is a VMworld 2017 US & Europe Platinum Sponsor. Enter the raffle to get the full conference pass. Pass includes the admission to all general and breakout sessions, VMware Hands-On Labs, Solutions Exchange, exclusive giveaways and the great VMworld Customer Appreciation Part

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
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.
Suggested Courses

636 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