有没有一种方法可以在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
‘,’附近的语法不正确。
问题是列名周围有多余的括号。这些都应该起作用:
-- 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问题时,您应该始终指定要查询的数据库。