Solved

Oracle CASE Statement

Posted on 2011-03-09
12
551 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
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
This is posted in an Oracle zone.  Is this Oracle or Informix?
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 100 total points
Comment Utility
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 73

Expert Comment

by:sdstuber
Comment Utility
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
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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 73

Expert Comment

by:sdstuber
Comment Utility
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
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.

 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
ah,  I see what you're saying now

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

0
 

Author Comment

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

TIA,
N
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 400 total points
Comment Utility
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 73

Expert Comment

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

Author Comment

by:Nakuru1234
Comment Utility
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
Comment Utility
Excellent answer and prompt help. 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

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 …
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

744 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

15 Experts available now in Live!

Get 1:1 Help Now