sql server - Nested Case statement in sql query -


my query

declare     @from       datetime='01 feb 2013' declare     @to         datetime='28 feb 2013 23:59:59'  select              case                   when                   (                         select top 1 f1.updatedon                         tickettypefollowup f1 with(nolock)                         f1.updatedon<t.updatedon , f1.ticket=t.ticket                         order f1.updatedon desc                   )is null                   ticket.ticketraisedon                    else                   (                        select top 1 f1.updatedon                        tickettypefollowup f1 with(nolock)                        f1.updatedon<t.updatedon , f1.ticket=t.ticket                        order f1.updatedon desc                   ) end [start date]        dbo.tickettypefollowup t with(nolock)       --some tables omitted                 cast(ticketraisedon date)between ''+convert(varchar(19), @from, 100)+''       , ''+convert(varchar(19), @to, 100)+'' 

if column [start date] has values greater @to @to value should print else [start date] column

how can write in same query ?

what tried

       case when(              case                   when                   (                         select top 1 f1.updatedon                         tickettypefollowup f1 with(nolock)                         f1.updatedon<t.updatedon , f1.ticket=t.ticket                         order f1.updatedon desc                   )is null                   ticket.ticketraisedon                    else                   (                        select top 1 f1.updatedon                        tickettypefollowup f1 with(nolock)                        f1.updatedon<t.updatedon , f1.ticket=t.ticket                        order f1.updatedon desc                   ) end [start date]               )> @to @to else [start date] end, 

db : sql server 2008

it looks me need this:

declare     @from       datetime='01 feb 2013' declare     @to         datetime='28 feb 2013 23:59:59'  select case when isnull(startdates.[start date], ticket.ticketraisedon) > @to @to else isnull(startdates.[start date], ticket.ticketraisedon) end resultvalue dbo.tickettypefollowup t with(nolock)     --some tables omitted      outer apply     (         select top 1 f1.updatedon [start date]         tickettypefollowup f1 with(nolock)         f1.updatedon<t.updatedon , f1.ticket=t.ticket         order f1.updatedon desc     ) startdates cast(ticketraisedon date)between ''+convert(varchar(19), @from, 100)+''       , ''+convert(varchar(19), @to, 100)+'' 

the outer apply lets specify computed value per row, referencing values joined tables above, after it's rather data coming join. give apply alias if table , access value many times needed above.

as can see, dramatically simplifies query-- apply extremely handy have in tool belt.)))

edit added isnull per comment.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -