我在此查询中遇到问题。我该如何解决这个问题:
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
我收到此错误:
关键字“top”附近的语法不正确。
有什么建议吗?
答案 0 :(得分:9)
尝试:
select top 1 case STATUS
而不是
select case top 1 STATUS
答案 1 :(得分:2)
您不需要嵌套选择。
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;
虽然这可以返回0行,但正确写入TOP 1的原始表单将始终返回一行,即使该值为NULL。
答案 2 :(得分: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
答案 3 :(得分: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
答案 4 :(得分: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