3

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?

APC
  • 144,005
  • 19
  • 170
  • 281
cihadakt
  • 3,054
  • 11
  • 37
  • 59

5 Answers5

12

Try:

select top 1 case STATUS

instead of

select case top 1 STATUS 
TechDo
  • 18,398
  • 3
  • 51
  • 64
4

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.

RichardTheKiwi
  • 105,798
  • 26
  • 196
  • 262
2
select (select top 1 (case 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 
AgeDeO
  • 3,137
  • 2
  • 25
  • 57
2

corrrect the case and top keyword

select (select top 1 case 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 
Sandeep Kumar
  • 783
  • 1
  • 5
  • 13
1
select top 1 * from (select case 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 
Anvesh
  • 7,103
  • 3
  • 45
  • 43