Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PL/SQL Multiple PAYER_IDs with PAYER_TYPES are these types

Posted on 2016-10-18
3
Medium Priority
?
60 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:Allen Pitts
  • 2
3 Comments
 

Author Comment

by:Allen Pitts
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 78

Accepted Solution

by:
slightwv (䄆 Netminder) earned 2000 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:Allen Pitts
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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
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…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
Suggested Courses

972 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