Solved

Nested Case Statement Syntax

Posted on 2013-01-03
8
802 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
[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
8 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 38741767
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 66

Expert Comment

by:Jim Horn
ID: 38741769
<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
ID: 38741772
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 45

Accepted Solution

by:
Kent Olsen earned 500 total points
ID: 38741792
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
 

Author Comment

by:nirajkrishna
ID: 38741797
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
ID: 38741809
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:Kent Olsen
ID: 38741812
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
ID: 38742184
This worked perfectly except for at the end I had to add one more "END" before the "END AS". Thanks!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

630 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