我发现,直接使用order-by时,没关系。
SELECT t0."D" AS fd,
SUM(t0."SD") AS top
FROM "mock_table_1" AS t0
GROUP BY t0."D"
ORDER BY top ASC
LIMIT 10
但在子查询中使用它时,会报告语法错误。
SELECT * FROM (
SELECT t0."D" AS fd,
SUM(t0."SD") AS top
FROM "mock_table_1" AS t0
GROUP BY t0."D"
ORDER BY top ASC
LIMIT 10
)
这是错误消息。
syntax error, unexpected ORDER, expecting UNION or EXCEPT or INTERSECT or ')' in: "select t0."A" as d0,
所以,我想知道monetdb的设计是这样的,还是一个bug?
答案 0 :(得分:4)
这是预期的行为。子查询中不允许使用offset,limit和order by
https://www.monetdb.org/pipermail/users-list/2013-October/006856.html
答案 1 :(得分:1)
符合SQL的DBMS不应该在子查询中允许ORDER BY,因为它与关系DBMS的概念模型相矛盾。参见:
Is order by clause allowed in a subquery
了解详情。然而,解决这个问题的方法是使用MonetDB does support的Window Functions。具体来说,在您的子查询中,而不是说,
SELECT c1 FROM t1;
你可以
SELECT c1, ROW_NUMBER() OVER () as rownum from t1;
现在您可以使用外部查询可用的内部查询结果的相对顺序。