?
Solved

oracle case statement

Posted on 2013-12-18
4
Medium Priority
?
421 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
  • 2
4 Comments
 
LVL 78

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 664 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 78

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 664 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 336 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
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…
Via a live example, show how to take different types of Oracle backups using RMAN.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.
Suggested Courses
Course of the Month9 days, 22 hours left to enroll

569 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