Solved

PL/SQL Multiple PAYER_IDs with PAYER_TYPES are these types

Posted on 2016-10-18
3
28 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
  • 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 76

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now