OVER ORDER BY 中的多个列
Posted
技术标签:
【中文标题】OVER ORDER BY 中的多个列【英文标题】:Multiple columns in OVER ORDER BY 【发布时间】:2012-07-26 07:45:42 【问题描述】:有没有办法在 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
',' 附近的语法不正确。
【问题讨论】:
【参考方案1】:问题是列名周围有多余的括号。这些都应该有效:
-- 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 问题时,您应该始终指定要查询的数据库。
【讨论】:
【参考方案2】:没有括号。
SELECT ROW_NUMBER() OVER(ORDER BY A.Col1, A.Col2) AS ID FROM MyTable A
【讨论】:
以上是关于OVER ORDER BY 中的多个列的主要内容,如果未能解决你的问题,请参考以下文章
Row_Number() OVER PARTITION BY 基于列中的值
OVER 子句中的 SQL ORDER BY 与 CLR 聚合不兼容?
rank() over(partition by A order by B) MySQL里可以partition多个字段嘛
如何与“row_number() over (partition by [Col] order by [Col])”相反