Solved

Oracle CASE Statement

Posted on 2011-03-09
12
584 Views
Last Modified: 2012-05-11
I know that the MDY() function within the SQL will fail … this tells Informix to return the Date value for the values sent.  Also CURRENT will need to be switched out to SYSDATE, but what will the SQL need to look like to correct the CASE statement below?

Name:
Nbr Closed this Qtr
Definition:
CASE WHEN Month(CURRENT) >= 1 AND Month(CURRENT) <= 3
THEN
SUM ( CASE WHEN Sfhd_Loan_Activity.Date_Of_Closing >= MDY(01,01,Year( CURRENT )-1) AND
Sfhd_Loan_Activity.Date_Of_Closing <= MDY(03,31,Year( CURRENT )-1)
     THEN 1 ELSE 0 END )
ELSE
SUM ( CASE WHEN Sfhd_Loan_Activity.Date_Of_Closing >= MDY(01,01,Year( CURRENT )) AND
      Sfhd_Loan_Activity.Date_Of_Closing <= MDY(03,31,Year( CURRENT ))
           THEN 1 ELSE 0 END )
END


TIA,
N
0
Comment
Question by:Nakuru1234
[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
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35087435
This is posted in an Oracle zone.  Is this Oracle or Informix?
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 100 total points
ID: 35087484
something like this, off the top of my head ...
CASE WHEN TO_NUMBER(TO_CHAR(SYSDATE,'MM')) >= 1 AND TO_NUMBER(TO_CHAR(SYSDATE, 'MM')) <= 3
THEN SUM ( CASE WHEN Sfhd_Loan_Activity.Date_Of_Closing >= TRUNC (TRUNC(SYSDATE, 'YYYY')-1 , 'YYYY') 
                                 AND Sfhd_Loan_Activity.Date_Of_Closing < ADD_MONTHS(3, TRUNC (TRUNC(SYSDATE, 'YYYY')-1 , 'YYYY'))
                       THEN 1 ELSE 0 END )
ELSE
SUM ( CASE WHEN Sfhd_Loan_Activity.Date_Of_Closing >= TRUNC(SYSDATE, 'YYYY')  
                         AND Sfhd_Loan_Activity.Date_Of_Closing <= ADD_MONTHS(3,  TRUNC(SYSDATE, 'YYYY'))
           THEN 1 ELSE 0 END )
END

Open in new window

0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35087493
try this...

CASE
           WHEN TO_NUMBER(TO_CHAR(SYSDATE, 'q')) = 1
           THEN
               SUM(
                   CASE
                       WHEN sfhd_loan_activity.date_of_closing >=
                                ADD_MONTHS(TRUNC(SYSDATE, 'yyyy'), -12)
                            AND sfhd_loan_activity.date_of_closing <=
                                    ADD_MONTHS(TRUNC(SYSDATE, 'yyyy'), -9) - 1
                       THEN
                           1
                       ELSE
                           0
                   END)
           ELSE
               SUM(
                   CASE
                       WHEN sfhd_loan_activity.date_of_closing >= TRUNC(SYSDATE, 'yyyy')
                            AND sfhd_loan_activity.date_of_closing <=
                                    ADD_MONTHS(TRUNC(SYSDATE, 'yyyy'), 3) - 1
                       THEN
                           1
                       ELSE
                           0
                   END)
       END

Open in new window

0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 74

Expert Comment

by:sdstuber
ID: 35087509
angeliii - your add_months parameters are backwards

also,  the end date is 3/31  so after adjusting by 3 months you to 4/1 you need to subtract 1
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35087536
you are surely correct about the add_months parameters ...
but :
<= MDY(03,31,Year( CURRENT )-1)
means to me the same as
< 1/4/ ....
or am I wrong there?
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35087602
MDY(03,31,Year( CURRENT )-1)


is equivalent to

to_date('03/31/' || to_char(sysdate,'yyyy')-1,'mm/dd/yyyy')

so

3/31/2010
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35087610
ah,  I see what you're saying now

 I didn't notice you switched the <= to <

0
 

Author Comment

by:Nakuru1234
ID: 35087684
So can you send me the correct SQL revised...am a little confused with all the comments.

TIA,
N
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 400 total points
ID: 35087695
http:#35087493

should be correct

try it with your table and see, we can't test it, we don't have your data
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35087702
if it doesn't work,  please provide sample data and expected output
0
 

Author Comment

by:Nakuru1234
ID: 35094257
When there is more data on the Oracle Instance I will try it and compare to Informix.  As of now it works for 1 qtr. Thanks for your help.

TIA,
N
0
 

Author Closing Comment

by:Nakuru1234
ID: 35094308
Excellent answer and prompt help. Thanks.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sybase and replication server 13 83
DB migration from Mssql to 12c oracle , data not loading. 3 52
Database Design Dilemma 6 71
set item in form 7 21
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
Via a live example, show how to take different types of Oracle backups using RMAN.

752 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