row_number分区结果错误

时间:2016-11-29 14:25:02

标签: sql window-functions

我使用ROW_NUMBER按销售额排名国家。查询如下所示

<Input onChange={e => alert(e.target.value)} />

由于某种原因,所有国家的排名都是一个。

1 个答案:

答案 0 :(得分:3)

如果按COUNTRY进行分区,则row_number将重置为1

SELECT country, 
       SUM(sales) AS sales, 
       ROW_NUMBER() OVER (ORDER BY SUM(sales) DESC) AS 'rank'
FROM table
GROUP BY country
ORDER BY sales DESC