Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Oracle CASE Statement

Posted on 2011-03-09
12
Medium Priority
?
596 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 400 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
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 1600 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

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

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 configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

661 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