Solved

Nested Case Statement Syntax

Posted on 2013-01-03
8
790 Views
Last Modified: 2013-01-03
I am trying to write a nested case statement to look back at the 3 prior months and give me an average. But I keep getting an error. Where am I wrong?



CASE
        WHEN LatestMonthNum=2
        THEN
            CASE
              WHEN MONTH(TodayDate)=1 and YEAR(TodayDate)=LatestYearNum or
              MONTH(TodayDate)=12 and YEAR(TodayDate)=LatestYearNum -1 or
              MONTH(TodayDate)=11 and YEAR(TodayDate)=LatestYearNum -1
               THEN ConvertedQuantity/3
             ELSE CASE WHEN LatestMonthNum=1 THEN
               CASE WHEN MONTH(TodayDate)=12 and YEAR(TodayDate)=LatestYearNum-1 or
              MONTH(TodayDate)=11 and YEAR(TodayDate)=LatestYearNum -1 or
              MONTH(TodayDate)=10 and YEAR(TodayDate)=LatestYearNum -1
               THEN ConvertedQuantity/3
              ELSE CASE WHEN MONTH(TodayDate)= LatestMonthNum-1 and YEAR(TodayDate)=LatestYearNum or MONTH(TodayDate)= LatestMonthNum-2 and YEAR(TodayDate)=LatestYearNum or MONTH(TodayDate)= LatestMonthNum-3 and YEAR(TodayDate)=LatestYearNum
             then ConvertedQuantity/3
             else null
           
             END as 'ThreeMoAvg'
0
Comment
Question by:nirajkrishna
8 Comments
 
LVL 45

Expert Comment

by:Kdo
Comment Utility
Howdy...

Indenting the code makes it really easy to see.  :)

CASE WHEN LatestMonthNum=2
     THEN CASE WHEN MONTH(TodayDate)=1 and YEAR(TodayDate)=LatestYearNum or
                    MONTH(TodayDate)=12 and YEAR(TodayDate)=LatestYearNum -1 or
                    MONTH(TodayDate)=11 and YEAR(TodayDate)=LatestYearNum -1
               THEN ConvertedQuantity/3
               ELSE CASE WHEN LatestMonthNum=1 
                         THEN CASE WHEN MONTH(TodayDate)=12 and YEAR(TodayDate)=LatestYearNum-1 or
                                        MONTH(TodayDate)=11 and YEAR(TodayDate)=LatestYearNum -1 or
                                        MONTH(TodayDate)=10 and YEAR(TodayDate)=LatestYearNum -1
                                   THEN ConvertedQuantity/3
                                   ELSE CASE WHEN MONTH(TodayDate)= LatestMonthNum-1 and YEAR(TodayDate)=LatestYearNum or
                                                  MONTH(TodayDate)= LatestMonthNum-2 and YEAR(TodayDate)=LatestYearNum or 
                                                  MONTH(TodayDate)= LatestMonthNum-3 and YEAR(TodayDate)=LatestYearNum
                                             then ConvertedQuantity/3
                                             else null
                                   END as 'ThreeMoAvg' 

Open in new window



It's really tough to see what you're trying to achieve.  Can you explain it?  The SQL should be easy.  :)


Kent
0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
<knee-jerk reaction>
I see AND and OR in the same clauses, but no parentheses ( ) to determine order of execution.  

I'm guessing the first block should be this:

CASE WHEN
    (MONTH(TodayDate)=1 and YEAR(TodayDate)=LatestYearNum) or
    (MONTH(TodayDate)=12 and YEAR(TodayDate)=LatestYearNum -1) or
    (MONTH(TodayDate)=11 and YEAR(TodayDate)=LatestYearNum -1)
      THEN ConvertedQuantity/3

Without the parentheses ( ), it's going to do each and .. or ... and as an entire set, and since Month(TodayDate) is likely a single value per row, it'll never evaluate as True and return the THEN expression, only the ELSE expression.
0
 

Author Comment

by:nirajkrishna
Comment Utility
This is just the calculated column and not the whole query. I am cross joined to a single row table that gives me the most recent date and  latest month number (LatestMonthNum) and latest year (LatestYearNum)
0
 
LVL 45

Accepted Solution

by:
Kdo earned 500 total points
Comment Utility
Every CASE clause must have a matching END.  The indented code above shows that a few well placed END statements are needed.  I think that you want something like this, but it's hard to know.  Keep in mind that if a CASE statement doesn't find a match, NULL is returned so this may take some more work.

CASE WHEN LatestMonthNum=2
     THEN CASE WHEN MONTH(TodayDate)=1 and YEAR(TodayDate)=LatestYearNum or
                    MONTH(TodayDate)=12 and YEAR(TodayDate)=LatestYearNum -1 or
                    MONTH(TodayDate)=11 and YEAR(TodayDate)=LatestYearNum -1
               THEN ConvertedQuantity/3
          END
     ELSE CASE WHEN LatestMonthNum=1 
               THEN CASE WHEN MONTH(TodayDate)=12 and YEAR(TodayDate)=LatestYearNum-1 or
                              MONTH(TodayDate)=11 and YEAR(TodayDate)=LatestYearNum -1 or
                              MONTH(TodayDate)=10 and YEAR(TodayDate)=LatestYearNum -1
                         THEN ConvertedQuantity/3
                    END
          END
     ELSE CASE WHEN MONTH(TodayDate)= LatestMonthNum-1 and YEAR(TodayDate)=LatestYearNum or
                    MONTH(TodayDate)= LatestMonthNum-2 and YEAR(TodayDate)=LatestYearNum or 
                    MONTH(TodayDate)= LatestMonthNum-3 and YEAR(TodayDate)=LatestYearNum
               then ConvertedQuantity/3
               else null
          END
END as 'ThreeMoAvg' 

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:nirajkrishna
Comment Utility
I am trying to say if the LatestMonthNum = 2 or 1 which means Jan or Feb then look at the latest month number and use the appropriate 3 prior months totals divided by 3 to give me an average. If it is not Jan or Feb then look at the data and when the month falls into the 3 prior months of today then add it to this column and divide it by 3 otherwise leave blank.


I am making a quick daily report that shows the current months performance (convertedQuantity) vs last month vs 3 prior month average.
0
 
LVL 15

Expert Comment

by:gplana
Comment Utility
as kdo said, every case should finish with an end. Try to change your expression by the expression added by kdo
0
 
LVL 45

Expert Comment

by:Kdo
Comment Utility
All that this complicated CASE statement does is divide ConvertedQuantity by 3.  Or not.

You probably want record selection (the WHERE filter) to determine which rows to process and just divide by 3.
0
 

Author Closing Comment

by:nirajkrishna
Comment Utility
This worked perfectly except for at the end I had to add one more "END" before the "END AS". Thanks!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

728 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

11 Experts available now in Live!

Get 1:1 Help Now