Solved

oracle case statement

Posted on 2013-12-18
4
411 Views
Last Modified: 2013-12-23
closed question

http://www.experts-exchange.com/Database/Oracle/Q_28320421.html

Requirements have changed.



select drive_date,division_cd,
       site_code,site_name,
         projection,min_projection,
         max_projection,actual_mobile_hrs
from hid_tab

I need help in a case statement for getting bed_nos. My requirements have changed.

If high_school_drive = 'Y' then
  if projection between min_projection and max_projection then
     I want to get no_of_beds
  end if;
elsif high_school_drive = 'N' then
  if projection between min_projection and max_projection then
    I want to get no_of_beds
  end if;
end if;
hid-case.txt
0
Comment
Question by:anumoses
[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
4 Comments
 
LVL 77

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 166 total points
ID: 39727645
I don't see the difference if high_school_drive = 'Y' or 'N'.  They both appear to provide no_of_beds if projection between min_projection and max_projection.
0
 
LVL 77

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 166 total points
ID: 39727648
I just looked at the other question you mentioned.  It seems like a straight forward case statement.  Are you having trouble understanding CASE statement syntax?
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 84 total points
ID: 39728068
as slightwv points out there does not seem to be any reason to evaluate high_school_drive, and in the sample data all projections are between the min and max too:
| DRIVE_DATE | DIVISION_CD | SITE_CODE | PROJECTION | MIN_PROJECTION | MAX_PROJECTION | NEW_CASE | PREV_CASE | ACTUAL_MOBILE_HOURS |
|------------|-------------|-----------|------------|----------------|----------------|----------|-----------|---------------------|
| 2013-01-02 |      Aurora |      MC90 |         25 |             16 |             25 |        3 |         3 |                   4 |
| 2013-01-02 |      Aurora |      MC90 |         25 |             21 |             25 |        5 |         3 |                   4 |
| 2013-01-02 |      Aurora |      MC90 |         25 |             21 |             30 |        3 |         3 |                   4 |
| 2013-01-02 |      Aurora |      WH02 |         31 |             31 |             40 |        6 |         6 |                   4 |
| 2013-01-02 |      Aurora |      WH02 |         31 |             31 |             40 |        6 |         6 |                   4 |
| 2013-01-02 |        Both |      IT28 |         10 |             10 |             10 |        3 |         3 |                   3 |
| 2013-01-02 |        Both |      IT28 |         10 |             10 |             15 |        3 |         3 |                   3 |
| 2013-01-02 |        Both |      NA06 |         31 |             31 |             40 |        6 |         6 |                   4 |
| 2013-01-02 |        Both |      NA06 |         31 |             31 |             40 |        6 |         6 |                   4 |
| 2013-01-02 | Tinley Park |      CH02 |         40 |             31 |             40 |        6 |         6 |                   4 |
| 2013-01-02 | Tinley Park |      CH02 |         40 |             31 |             40 |        6 |         6 |                   4 |
| 2013-01-02 | Tinley Park |      OP04 |         31 |             31 |             40 |        6 |         6 |                   4 |
| 2013-01-02 | Tinley Park |      OP04 |         31 |             31 |             40 |        6 |         6 |                   4 |
| 2013-01-03 |      Aurora |      BG18 |         21 |             16 |             25 |        3 |         3 |                   4 |
| 2013-01-03 |      Aurora |      BG18 |         21 |             21 |             25 |        5 |         3 |                   4 |
| 2013-01-03 |      Aurora |      BG18 |         21 |             21 |             30 |        3 |         3 |                   4 |
| 2013-01-03 |        Both |      5516 |         31 |             31 |             40 |        6 |         6 |                   4 |
| 2013-01-03 |        Both |      5516 |         31 |             31 |             40 |        6 |         6 |                   4 |
| 2013-01-03 | Tinley Park |      HO37 |         21 |             16 |             25 |        3 |         3 |                   4 |
| 2013-01-03 | Tinley Park |      HO37 |         21 |             21 |             25 |        5 |         3 |                   4 |
| 2013-01-03 | Tinley Park |      HO37 |         21 |             21 |             30 |        3 |         3 |                   4 |
| 2013-01-03 | Tinley Park |      NL28 |         35 |             31 |             40 |        6 |         6 |                   4 |
| 2013-01-03 | Tinley Park |      NL28 |         35 |             31 |             40 |        6 |         6 |                   4 |
| 2013-01-04 |        Both |      LI16 |         31 |             10 |             35 |        3 |         3 |                   7 |
| 2013-01-04 |        Both |      LI16 |         31 |             31 |             40 |        3 |         3 |                   7 |
| 2013-01-04 |        Both |      LP05 |         23 |             16 |             25 |        3 |         3 |                   4 |
| 2013-01-04 |        Both |      LP05 |         23 |             21 |             25 |        5 |         3 |                   4 |
| 2013-01-04 |        Both |      LP05 |         23 |             21 |             30 |        3 |         3 |                   4 |

Open in new window

produced by this query:
SELECT
      to_char(drive_date,'YYYY-MM-DD') as drive_date
    , division_cd
    , site_code
--    , site_name

    , projection
    , min_projection
    , max_projection

    , CASE
        WHEN projection BETWEEN min_projection AND max_projection THEN no_of_beds 
        ELSE 0 
      END AS new_case

       , CASE 
           WHEN actual_mobile_hours <= 4.5 
              THEN CASE 
                   WHEN projection BETWEEN 10 AND 30 THEN 3 
                   WHEN projection BETWEEN 31 AND 60 THEN 6 
                   WHEN projection BETWEEN 61 AND 90 THEN 9 
                   WHEN projection BETWEEN 91 AND 120 THEN 12 
                   WHEN projection BETWEEN 121 AND 150 THEN 15 
               END 
           WHEN actual_mobile_hours > 5 
              THEN CASE WHEN projection BETWEEN 10 AND 40 THEN 3 
                   WHEN projection BETWEEN 11 AND 75 THEN 6 
                   WHEN projection BETWEEN 76 AND 115 THEN 9 
                   WHEN projection BETWEEN 116 AND 150 THEN 12 
                   WHEN projection BETWEEN 151 AND 190 THEN 15 
               END 
        END AS prev_case

    , actual_mobile_hours

FROM hid_tab

--http://sqlfiddle.com/#!4/84e6a/9

Open in new window

0
 
LVL 6

Author Closing Comment

by:anumoses
ID: 39735979
Requirements keep changing. Just had to close this.. Thanks,
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to recover a database from a user managed backup
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

688 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