views:

22711

answers:

5

I'm currently writing an SQL Query, where a few of the columns returned need to be calculated depending on quite a lot of conditions.

I'm currently using nested case statements, but its getting messy. Is there a better (more organised and/or readable) way?

(I am using Microsoft SQL Server, 2005)


A simplified example:

SELECT
    col1,
    col2,
    col3,
    CASE
        WHEN condition 
        THEN
            CASE
                WHEN condition1 
                THEN
                    CASE 
                        WHEN condition2
                        THEN calculation1
                        ELSE calculation2
                    END
                ELSE
                    CASE 
                        WHEN condition2
                        THEN calculation3
                        ELSE calculation4
                    END
            END
        ELSE 
            CASE 
                WHEN condition1 
                THEN 
                    CASE
                        WHEN condition2 
                        THEN calculation5
                        ELSE calculation6
                    END
                ELSE
                    CASE
                        WHEN condition2 
                        THEN calculation7
                        ELSE calculation8
                    END
            END            
    END AS 'calculatedcol1',
    col4,
    col5 -- etc
FROM table
+4  A: 

a user-defined function may server better, at least to hide the logic - esp. if you need to do this in more than one query

Steven A. Lowe
+7  A: 

You could try some sort of COALESCE trick, eg:

SELECT COALESCE(
  CASE WHEN condition1 THEN calculation1 ELSE NULL END,
  CASE WHEN condition2 THEN calculation2 ELSE NULL END,
  etc...
)
Chris KL
Perfect, thanks :)
Sophia
Nice, I just tested to ensure that it short-circuits, and was suprised to find that it does. So if condition1 checked for a divide by zero, it appears that it's safe to do it in condition2. Not sure if this is guaranteed.
Cade Roux
One catch is that if one of your cases legitimately wants to return a NULL, it's no longer possible.
Chris KL
+1  A: 

I did this in a project in the past, but have since regretted it. It would be easier for me to manage in .NET.

So, I hope to pull most of this logic out of my SQL stored procs and just make a simple select, process, update sequence from SQL to .NET to SQL.

This would work for me because this is just an overnight SQL job and I don't care for the performance hit.

EndangeredMassa
That makes a lot of sense, although it wouldn't suit my situation because the query will be used in reports.Thanks though :)
Sophia
+1  A: 

I personally do it this way, keeping the embedded CASE expressions confined. I'd also put comments in to explain what is going on. If it is too complex, break it out into function.

SELECT
    col1,
    col2,
    col3,
    CASE WHEN condition THEN
      CASE WHEN condition1 THEN
        CASE WHEN condition2 THEN calculation1
        ELSE calculation2 END
      ELSE
        CASE WHEN condition2 THEN calculation3
        ELSE calculation4 END
      END
    ELSE CASE WHEN condition1 THEN 
      CASE WHEN condition2 THEN calculation5
      ELSE calculation6 END
    ELSE CASE WHEN condition2 THEN calculation7
         ELSE calculation8 END
    END AS 'calculatedcol1',
    col4,
    col5 -- etc
FROM table
beach
A: 

Here's a simple solution to the nested "Complex" case statment: --Nested Case Complex Expression select datediff(dd,Invdate,'2009/01/31')+1 as DaysOld , case when datediff(dd,Invdate,'2009/01/31')+1 >150 then 6 else case when datediff(dd,Invdate,'2009/01/31')+1 >120 then 5 else case when datediff(dd,Invdate,'2009/01/31')+1 >90 then 4 else case when datediff(dd,Invdate,'2009/01/31')+1 >60 then 3 else case when datediff(dd,Invdate,'2009/01/31')+1 >30 then 2 else case when datediff(dd,Invdate,'2009/01/31')+1 >30 then 1 end end end end end end as Bucket from rm20090131atb

Just make sure you have an end statement for every case statement

You may want to format your answer if you want it to be read by others.
Ismail