我一直在与这个查询作斗争2天,但仍无法找到问题所在。
SQL查询是这样的:
select t2.transno,
case
when (Select Count (*)
From v1
where v1.transno < v1.transno) > 1
then (
case
when (Select Top 1 v1.transno
From v1
Where v1.transno < v1.transno
Order By v1.transno Desc)=(T2.transno-1)
then ' '
else 'Missing Invoice')
else ''
end AS Marker
FROM v1 AS T2
它给了我错误信息:
Msg 102, Level 15, State 1, Line 15 Incorrect syntax near ')'.
答案 0 :(得分:1)
我猜你忘了把这句话放在最后:else 'Missing Invoice')
试试else 'Missing Invoice') end
select t2.transno,
case
when (Select Count (*)
From v1
where v1.transno < v1.transno) > 1
then (
case
when (Select Top 1 v1.transno
From v1
Where v1.transno < v1.transno
Order By v1.transno Desc)=(T2.transno-1)
then ' '
else 'Missing Invoice'
end )
else ''
end AS Marker
FROM v1 AS T2
答案 1 :(得分:0)
你的第二个案例陈述缺少它需要的结束。试试这个:
select t2.transno,
case
when (Select Count (*)
From v1
where v1.transno < v1.transno) > 1
then (
case
when (Select Top 1 v1.transno
From v1
Where v1.transno < v1.transno
Order By v1.transno Desc)=(T2.transno-1)
then ' '
else 'Missing Invoice') end
else ''
end AS Marker
FROM v1 AS T2
答案 2 :(得分:0)
我不知道这是否是答案,但我认为Top 1
应为Top (1)
。 (See here)
答案 3 :(得分:0)
v1.transno < v1.transno
始终为false =零行。
因此您的查询可以重写为
SELECT v1.transno, 'Missing Invoice' AS Marker FROM v1
更严重的是,它可以更简单地表达为
select t2.transno,
case
when foo.maxtransno IS NULL THEN 'Missing Invoice'
ELSE ' '
END AS Marker
FROM
v1 AS T2
OUTER APPLY
(Select MAX(v1.transno) AS maxtransno
From v1
Where v1.transno < t2.transno
HAVING MAX(v1.transno) = T2.transno-1
) foo