SQL Anywhere中的MAX子查询返回错误

时间:2018-02-10 19:54:00

标签: sql max sybase greatest-n-per-group sqlanywhere

在sqlanywhere 12中,我编写了以下查询,该查询返回两行数据:

SELECT "eDatabase"."Vendor"."VEN_CompanyName", "eDatabase"."OrderingInfo"."ORD_Timestamp"
    FROM "eDatabase"."OrderingInfo" 
    JOIN "eDatabase"."Vendor" 
        ON "eDatabase"."OrderingInfo"."ORD_VEN_FK" = "eDatabase"."Vendor"."VEN_PK"
    WHERE ORD_INV_FK='7853' AND ORD_DefaultSupplier = 1

返回:

'**United Natural Foods IN','2018-02-07 15:05:15.513'
'Flora                    ','2018-02-07 14:40:07.491'

我想只返回“ORD_Timestamp”列中具有最大时间戳的行。在简单地尝试通过MAX(“eDatabase”。“OrderingInfo”。“ORD_Timestamp”)进行选择之后,我发现了许多帖子,描述了该方法不起作用以及使用子查询来获得结果。

我很难以一种有效的方式创建子查询,并且使用以下查询我在上一次“ON”时遇到语法错误:

SELECT "eDatabase"."Vendor"."VEN_CompanyName", "eDatabase"."OrderingInfo"."ORD_Timestamp"
FROM ( "eDatabase"."OrderingInfo"
JOIN 
    "eDatabase"."OrderingInfo" 
    ON "eDatabase"."Vendor"."VEN_PK" = "eDatabase"."OrderingInfo"."ORD_VEN_FK" )
INNER JOIN 
    (SELECT "eDatabase"."Vendor"."VEN_CompanyName", MAX("eDatabase"."OrderingInfo"."ORD_Timestamp") 
    FROM "eDatabase"."OrderingInfo") 
    ON "eDatabase"."Vendor"."VEN_PK" = "eDatabase"."OrderingInfo"."ORD_VEN_FK"
WHERE ORD_INV_FK='7853' AND ORD_DefaultSupplier = 1

有谁知道我如何调整它以使查询正确选择最大ORD_Timestamp行?

1 个答案:

答案 0 :(得分:0)

试试这个:

    SELECT TOP 1 "eDatabase"."Vendor"."VEN_CompanyName", "eDatabase"."OrderingInfo"."ORD_Timestamp"
        FROM "eDatabase"."OrderingInfo" 
        JOIN "eDatabase"."Vendor" 
            ON "eDatabase"."OrderingInfo"."ORD_VEN_FK" = "eDatabase"."Vendor"."VEN_PK"
        WHERE ORD_INV_FK='7853' AND ORD_DefaultSupplier = 1
order by "ORD_Timestamp" desc

这是他们最大的命令,并且只说hsow的顶行