gpt4 book ai didi

sql - OVER ORDER BY 中的多个列

转载 作者:行者123 更新时间:2023-12-02 13:02:31 26 4
gpt4 key购买 nike

有没有办法在 OVER ORDER BY 子句中指定多个列?

SELECT ROW_NUMBER() OVER(ORDER BY (A.Col1)) AS ID FROM MyTable A

以上工作正常,但尝试添加第二列不起作用。

SELECT ROW_NUMBER() OVER(ORDER BY (A.Col1, A.Col2)) AS ID FROM MyTable A

Incorrect syntax near ','.

最佳答案

问题是列名周围有多余的括号。这些应该都有效:

-- The standard way
SELECT ROW_NUMBER() OVER(ORDER BY A.Col1) AS ID FROM MyTable A
SELECT ROW_NUMBER() OVER(ORDER BY A.Col1, A.Col2) AS ID FROM MyTable A

-- Works, but unnecessary
SELECT ROW_NUMBER() OVER(ORDER BY (A.Col1), (A.Col2)) AS ID FROM MyTable A

此外,当您提出 SQL 问题时,您应该始终指定要查询的数据库。

关于sql - OVER ORDER BY 中的多个列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11691046/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com