Solved

oracle case statement

Posted on 2013-12-18
4
405 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 76

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 76

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 48

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

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

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
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…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
Via a live example, show how to take different types of Oracle backups using RMAN.

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

9 Experts available now in Live!

Get 1:1 Help Now