Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

top 1 case in select statement on TSQL

I am having problem on this query. How can I fix this:

select (select case top 1 STATUS 
       when 'Inprocess' then 'Processing' 
       when 'Inworkbin' then 'Waiting In Draft' 
       end 
    from ICS_EMAIL_CONNECTIONS_TRX A    
    where A.SESSIONID = B.SESSIONID 
    and STATUS <> 'Completed'
    order by A.CREATE_DATE desc) as LAST_STATUS 

I am getting this error:

Incorrect syntax near the keyword 'top'.

Any suggestions?

like image 314
cihadakt Avatar asked Nov 23 '25 06:11

cihadakt


2 Answers

Try:

select top 1 case STATUS

instead of

select case top 1 STATUS 
like image 63
TechDo Avatar answered Nov 25 '25 20:11

TechDo


You don't need the nested select.

select top 1 case STATUS 
             when 'Inprocess' then 'Processing' 
             when 'Inworkbin' then 'Waiting In Draft' 
             end LAST_STATUS
from ICS_EMAIL_CONNECTIONS_TRX A    
where A.SESSIONID = B.SESSIONID 
and STATUS <> 'Completed'
order by A.CREATE_DATE desc;

Although this can return 0 rows whereas your original form with the TOP 1 written properly will always return one row, even if the value is NULL.

like image 40
RichardTheKiwi Avatar answered Nov 25 '25 18:11

RichardTheKiwi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!