Solved

PL/SQL Multiple PAYER_IDs with PAYER_TYPES are these types

Posted on 2016-10-18
3
53 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to recover a database from a user managed backup
Suggested Courses

624 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