Solved

PL/SQL Multiple PAYER_IDs with PAYER_TYPES are these types

Posted on 2016-10-18
3
48 Views
Last Modified: 2016-10-19
Hello expert,

Thanks to slightWv for the excellent solution copied herewith below
marked '-- Two PAYER_TYPE_CODES'.

Successfully extended  
..................
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(case when payer_type_code = 17003 then 1 end) has_17003,
             count(*) total_count
       from CCM_PAYER_TYPE
       group by payer_id
 )
 where total_count=3 and has_17001=1 and has_17002=1 and has_17003=1

to three PAYER_TYPE_CODE s

But when the pattern was used to extend to four PAYER_TYPE_CODE s,
marked  "-- Two PAYER_TYPE_CODES" copied herewith below,
the query returns zero rows.

Why will the pattern not extend past three?

Thanks.


Allen in Dallas



-- Two PAYER_TYPE_CODES
  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


-- Four PAYER_TYPE_CODES
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(case when payer_type_code = 17003 then 1 end) has_17003,
             count(case when payer_type_code = 17004 then 1 end) has_17004,
             count(*) total_count
       from CCM_PAYER_TYPE
       group by payer_id
 )
 where total_count=4 and has_17001=1 and has_17002=1 and has_17003=1 and has_17004=1
 )
 SELECT
 CTE.PAYER_ID,
 PAYER_TYPE_CODE
 from CTE
 join CCM_PAYER_TYPE PAT on CTE.PAYER_ID = PAT.PAYER_ID
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
  • 2
3 Comments
 

Author Comment

by:9apit
ID: 41849101
Hello Expert,

I have discovered why the extension of the ....count(case... statement did
not extend to four cases. The data did not have four cases, at least not where
the PAYER_TYPE s were 17001, 17002, 17003, 17004.

So tried group by PAYER_ID where count(*) =4 and found a payer with four PAYER_TYPE s
and then reworked the ....count(case... using those PAYER_TYPE s. So it would seem
reasonable that the ...count(case... construct does work.

Thanks.

Allen in Dallas
0
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 41849155
A lot depends on your exact requirements.  I took the previous question to mean you want payers with ONLY TWO codes and one each of 17001 and 17002.

If you look at my test data, there were others that had two codes but they weren't one each of the two you provided.

If you remove the count and case you might get payers with 4 of all 17001's.

If that doesn't matter, then the query can probably be even simpler.

We just need to know the exact requirements.
0
 

Author Closing Comment

by:9apit
ID: 41850156
Hello SlightWV,

Excellent answer. Yes you are correct. The requirement s are
1. That there be two PAYER_TYPE_CODE s
and
2. Those two codes should be 17001and 17002

Also used suggestions on eliminating the ' WithCTE as'

Thanks.

Allen
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Checking for column width 8 49
Updating a temp table inside a PL/SQL block 3 65
Oracle Date add 9 35
Multiple Rows in a table for Separate Columns 12 25
This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
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…
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 Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

752 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