Solved

oracle case statement

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
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 setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

749 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