tags:

views:

62

answers:

1

does anyone know whats wrong with this nested select stament? It complains about missing )'s but i can't unerstand why it doesn't work (i've left off the other bits of the statment)

Select
(CASE WHEN REQUESTS.grade_id = 1 THEN
      (CASE WHEN  ((date_completed-date_submitted)*24*60)<=30 THEN 'Yes'
           ELSE 'No'
      END)
 ELSE CASE WHEN REQUESTS.grade_id = 2 THEN
      (CASE ((date_completed-date_submitted)*24*60) <=120 THEN 'Yes'
           ELSE 'No'
      END) 
 ELSE CASE WHEN REQUESTS.grade_id = 3 THEN
     (CASE ((date_completed-date_submitted)*24*60)<=14400 THEN 'Yes'
          ELSE 'No'
     END)
 END)in_SLA

If i just do

    Select
       (CASE WHEN REQUESTS.grade_id = 1 THEN
           (CASE WHEN  ((date_completed-date_submitted)*24*60)<=30 THEN 'Yes'
               ELSE 'No'
            END)
       END) in_sla

It works fine!

any help is much appreciated

M

sorry being a tard i'm missing the whens from the nested cases

+4  A: 

It should be:

Select
(CASE WHEN REQUESTS.grade_id = 1 THEN
      (CASE WHEN  ((date_completed-date_submitted)*24*60)<=30 THEN 'Yes'
           ELSE 'No'
      END)
      WHEN REQUESTS.grade_id = 2 THEN
      (CASE ((date_completed-date_submitted)*24*60) <=120 THEN 'Yes'
           ELSE 'No'
      END) 
      WHEN REQUESTS.grade_id = 3 THEN
     (CASE ((date_completed-date_submitted)*24*60)<=14400 THEN 'Yes'
          ELSE 'No'
     END)
 END)in_SLA

i.e. just "WHEN" not "ELSE CASE WHEN" for each case.

I'd be tempted to simplify to:

Select
CASE WHEN (REQUESTS.grade_id = 1 AND (date_completed-date_submitted)*24*60 <= 30)
       OR (REQUESTS.grade_id = 2 AND (date_completed-date_submitted)*24*60 <=120)
       OR (REQUESTS.grade_id = 3 AND (date_completed-date_submitted)*24*60 <=14400)
     THEN 'Yes'
     ELSE 'No'
 END in_SLA
Tony Andrews
Or even better: `CASE REQUESTS.grade_id WHEN 1 THEN ... WHEN 2 THEN ... WHEN 3 THEN ... END`
Guffa
thanks but this doesnt work either, the Else Case When bit came in from me messing around tring to get it to work.any other ideas?
matt1234
i'm missing the When from the nested cases!!sorry being a tard!
matt1234
That's OK. We've all done the same.
Brian Hooper