可以在Sybase ASE中使用具有"TOP 1列"的相关子查询吗?

DVK*_*DVK 5 sql subquery sybase-ase correlated-subquery

我试图在Sybase ASE 12上使用建议的查询,它抱怨语法错误.

SELECT 
    item, 
    ( SELECT TOP 1 tags.tag
      FROM #tags tags
        LEFT JOIN t o
          ON  tags.tag = o.tag
          AND o.item_id = n.item_id 
      WHERE o.tag IS NULL
      ORDER BY tags.tag
    ) 'tag',
    value  
FROM
    t_new n
Run Code Online (Sandbox Code Playgroud)

错误: Incorrect syntax near the keyword 'top'.

但是,当我用MAX()替换(TOP 1 tag... ORDER BY tag)时,同样的查询有效:

SELECT 
    item, 
    ( SELECT max(tags.tag)
      FROM #tags tags
        LEFT JOIN t o
          ON  tags.tag = o.tag
          AND o.item_id = n.item_id 
      WHERE o.tag IS NULL
        --  ORDER BY tags.tag
    ) 'tag',
    value  
FROM
    t_new n
Run Code Online (Sandbox Code Playgroud)
  • 为什么在Sybase的相关子查询中使用(TOP 1 tag... ORDER BY tag)问题?

  • 是否有任何修复原始查询不使用min()/ max()?

Mic*_*ner 5

Adaptive Server Enterprise 12.5.3版支持外部查询select语句中的top n子句,但不支持子查询的select列表中的top n子句.这与Microsoft SQL Server不同.在子查询中对Adaptive Server使用top n子句的任何尝试都会产生语法错误.

从ASE 12.5.3文档到这里