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

标签 sql subquery sap-ase correlated-subquery

我尝试使用 proposed query在 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

错误: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
  • 为什么在 Sybase 的相关子查询中使用 ( TOP 1 tag ... ORDER BY tag ) 是个问题?
  • 不使用 min()/max() 的原始查询是否有任何修复?
  • 最佳答案

    Adaptive Server Enterprise version 12.5.3 supports the top n clause in outer query select statements, but not in the select list of a subquery. This differs from Microsoft SQL Server. Any attempt to use the top n clause with Adaptive Server in a subquery yields a syntax error.



    来自 ASE 12.5.3 文档 here

    关于sql - 可以在 Sybase ASE 中使用具有 "TOP 1 column"的相关子查询吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14860728/

    相关文章:

    mysql - SQL 列出所有重复项

    mysql - 在 MYSQL 查询中使用 If

    php - Mysql 子选择查询优化

    sql - 如何在 PL/SQL 中加入子查询?

    java - 如何远程连接 ddlgen

    sql - 在另一个表上的同一表的两列上进行内连接,包括 OR 条件

    sql - 使用 Scala 中的数据帧在 Spark 1.30 中保存为文本

    .net - 强类型数据集中子查询的计算

    ado.net - 适用于 Sybase ASE 的 Windows 8 OLEDB/ADO.NET 驱动程序?

    java - 如何管理Web服务调用和数据库之间的分布式事务协调?