Avatar of Allen Pitts
Allen PittsFlag for United States of America

asked on 

PL/SQl Expanding the WHERE statement in query

Hello expert,

Looking for PROTOCOL_ID associated with Employers linked to a certain Payer.

In the CCM_PROTOCOL table, where the OWNER_TYPE_CODE = 15001 (employers),
the OWNER_ID is the EMPLOYER_ID in the ASSOCIATION table.

So the query says: Show me the Protocols where the OWNER_ID
is in the subset of Employers associated with a Payer in the
Association table.

A query was drafted to do this and is copied here with below
marked '-- Cross Insurance Injury care Protocols' and works well.

Then the boss says 'besides Employers add Programs and Locations'.

So the WHERE  statement was expanded to include Programs (When I get Programs to
work will add Locations in a similar fashion.)

..........
where owner_id in
(SELECT
  EMPLOYER_ID
    FROM CCM.CCM_ASSOCIATION
  where payer_ID = 117413
  and IS_DISABLED = 0)
OR
(SELECT
  PROGRAM_ID
    FROM CCM.CCM_ASSOCIATION
  where payer_ID = 117413
  and IS_DISABLED = 0)
and  PROTOCOL_TYPE_CODE = 22001
and
(OWNER_TYPE_CODE =15001 -- employers
OR
OWNER_TYPE_CODE =15006) -- programs
and IS_DISABLED = 0  ;

How can I create a subset of Employers and Programs
from which to select Protocols?

Thanks.

Allen in Dallas

-- Cross Insurance Injury care Protocols
SELECT PROTOCOL_ID,
   PROTOCOL_NAME,
  PROTOCOL_TYPE_CODE,
    IS_DISABLED,
  OWNER_ID,
  OWNER_TYPE_CODE
FROM CCM.CCM_PROTOCOL
where owner_id in
(SELECT
  EMPLOYER_ID
    FROM CCM.CCM_ASSOCIATION
  where payer_ID = 117413
  and IS_DISABLED = 0)
and  PROTOCOL_TYPE_CODE = 22001
and OWNER_TYPE_CODE =15001
and IS_DISABLED = 0  ;
Oracle Database

Avatar of undefined
Last Comment
Allen Pitts
ASKER CERTIFIED SOLUTION
Avatar of Arana (G.P.)
Arana (G.P.)

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Arana (G.P.)
Arana (G.P.)

you could also try using EXISTS instead of IN, will work faster in case Id's are indexed

SELECT PROTOCOL_ID
	,PROTOCOL_NAME
	,PROTOCOL_TYPE_CODE
	,IS_DISABLED
	,OWNER_ID
	,OWNER_TYPE_CODE
FROM CCM.CCM_PROTOCOL
WHERE 
PROTOCOL_TYPE_CODE = 22001
	AND (
		OWNER_TYPE_CODE = 15001
		OR OWNER_TYPE_CODE = 15006
		)
	AND IS_DISABLED = 0
	AND
	exists (
		SELECT EMPLOYER_ID
		FROM CCM.CCM_ASSOCIATION
		WHERE payer_ID = 117413
			AND IS_DISABLED = 0
			AND ccm.ccm_protocol.owner_id=CCM.CCM_ASSOCIATION.owner_id
		);

Open in new window

Avatar of Allen Pitts
Allen Pitts
Flag of United States of America image

ASKER

Well done. Thanks.
Oracle Database
Oracle Database

Oracle is an object-relational database management system. It supports a large number of languages and application development frameworks. Its primary languages are SQL, PL/SQL and Java, but it also includes support for C and C++. Oracle also has its own enterprise modules and application server software.

81K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo