Solved

Case Statement

Posted on 2014-07-18
4
284 Views
Last Modified: 2014-07-21
Hello,

Please assist with understanding this case statement. Let me know if any additional information is needed.
where
ca.addressid =
(
  case
    when (select count(*) from dss.contactaddress where contactid = ca.contactid and addresstype='Home' and program = ca.program) =0
      then (case
              when (select count(*) from dss.contactaddress where contactid = ca.contactid and preferred='1' and program = ca.program) =0
                then (select min(addressid) from dss.contactaddress where contactid = ca.contactid and program = ca.program)
                else (select min(addressid) from dss.contactaddress where contactid = ca.contactid and preferred='1' and program = ca.program)
            end)
      else (select min(addressid) from dss.contactaddress where contactid = ca.contactid and addresstype='Home' and program = ca.program)    
  end)
0
Comment
Question by:jverasql
  • 2
4 Comments
 
LVL 73

Expert Comment

by:sdstuber
ID: 40205091
If the  number of Home addresses is 0 and the number of addresses with Preferred = 1 is 0 then
return rows where the addressid is the MINIMUM address id for that contact and program

If the number of Home addresses is 0 and the number of addresses with preferred = 1 is NOT 0 then
return rows where the address is the MINIMUM address id where preferred = 1 for that contact and program

if the number of Home addresses is NOT 0 then
return rows where the address is the MINIMUM address id for Home addresses for that contact and program
0
 
LVL 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 150 total points
ID: 40205095
The general syntax of the CASE statement you have is:
CASE
   WHEN "this"="this" THEN this
   WHEN "that"="that" THEN that
   ELSE something_else
END

If you break the giant case down and indent properly, you'll see you just have nested case statements.

What parts are confusing?
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 350 total points
ID: 40205875
WHERE ca.addressid =
      (
      CASE
            WHEN (                                     -- CHECK for HOME ADDRESS
                  SELECT
                        COUNT(*)
                  FROM dss.contactaddress
                  WHERE contactid = ca.contactid
                        AND addresstype = 'Home'
                        AND program = ca.program
            )
            = 0 THEN (CASE                             -- if NO home address
                  WHEN (                                    -- CHECK for PREFERRED ADDRESS
                        SELECT
                              COUNT(*)
                        FROM dss.contactaddress
                        WHERE contactid = ca.contactid
                              AND preferred = '1'
                              AND program = ca.program
                  )                                         -- if no preferred address
                  = 0 THEN (                                -- USE first available ADDRESS
                        SELECT
                              MIN(addressid)
                        FROM dss.contactaddress
                        WHERE contactid = ca.contactid
                              AND program = ca.program
                  )                
                  ELSE (                                    -- use first preferred ADDRESS
                        SELECT
                              MIN(addressid)
                        FROM dss.contactaddress
                        WHERE contactid = ca.contactid
                              AND preferred = '1'
                              AND program = ca.program
                  ) END)
            ELSE (                                    -- There IS a home address so use it
                  SELECT
                        MIN(addressid)
                  FROM dss.contactaddress
                  WHERE contactid = ca.contactid
                        AND addresstype = 'Home'
                        AND program = ca.program
            ) END)

Open in new window

0
 
LVL 73

Expert Comment

by:sdstuber
ID: 40209804
jverasql - can I ask why a split didn't include the first post - did it not answer your question?  I does (I think) explain exactly what your nested case statements do.  If not, what's missing?
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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle regular expression 6 45
Oracle Next Available Number 2 31
Oracle - Create Procedure with Paramater 16 56
SQL query question 8 35
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…
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…
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 Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

911 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

17 Experts available now in Live!

Get 1:1 Help Now