如何在DB2中仅与UNION ALL结合使用FETCH FIRST 1 ROWS ?



见标题。这就是我正在尝试的:

select a.work_order_no 
from (
select work_order_no as work_order_no 
from work_order_line 
where insert_timestamp is not null 
FETCH FIRST 1 ROWS ONLY 
union all
select work_order_no as work_order_no 
from work_order_line 
where insert_timestamp is null 
FETCH FIRST 1 ROWS ONLY 
) as a
FETCH FIRST 1 ROWS ONLY

但是它给出了以下错误:

SQL State: 42601 Vendor Code: -199 Message: [SQL0199] Keyword UNION not expected. Valid tokens: ). Cause . . . . . :   The keyword UNION was not expected here.  A syntax error was detected at keyword UNION.  The partial list of valid tokens is ). This list assumes that the statement is correct up to the unexpected keyword.  The error may be earlier in the statement but the syntax of the statement seems to be valid up to this point. Recovery  . . . :   Examine the SQL statement in the area of the specified keyword.  A colon or SQL delimiter may be missing. SQL requires reserved words to be delimited when they are used as a name. Correct the SQL statement and try the request again.  Processing ended because the highlighted statement did not complete successfully  Failed statements: 1

在SQL中,这个概念将与'top 1'语法一起工作。我假设这也可以在DB2中工作,但我只是做错了语法顺序?

我问过一位同事,幸运的是他很快就回复了:

I missed some ()

select a.work_order_no 
from (
(select work_order_no as work_order_no 
from work_order_line 
where insert_timestamp is not null 
FETCH FIRST 1 ROWS ONLY)
union all
(select work_order_no as work_order_no 
from work_order_line 
where insert_timestamp is null 
FETCH FIRST 1 ROWS ONLY )
) as a
FETCH FIRST 1 ROWS ONLY

最新更新