SQL 分析函数:rank() over partition by not working property

Posted

技术标签:

【中文标题】SQL 分析函数:rank() over partition by not working property【英文标题】:SQL analytical function: rank() over partition by not working property 【发布时间】:2015-03-24 20:38:32 【问题描述】:
CREATE TABLE customers
( customer_id number(10) not null,
  customer_name varchar2(50) not null
);

INSERT INTO customers VALUES(22,'W');
INSERT INTO customers VALUES(22,'W');
INSERT INTO customers VALUES(20,'Q');
INSERT INTO customers VALUES(20,'Q');
COMMIT;

现在我正在尝试按客户名称获取与我的分区相对应的不同等级

SELECT DENSE_RANK() OVER(PARTITION BY customer_name ORDER BY CUSTOMER_ID) , CUSTOMER_ID FROM CUSTOMERS;

输出:

1   20
1   20
1   22
1   22

预期输出:

1   20
1   20
2   22
2   22

【问题讨论】:

【参考方案1】:

使用下面的查询

SELECT 
DENSE_RANK() OVER(ORDER BY CUSTOMER_ID) , 
CUSTOMER_ID 
FROM CUSTOMERS;

删除 partition BY by 子句,因为如果您使用 partition by 子句,它将根据名称划分结果。

【讨论】:

以上是关于SQL 分析函数:rank() over partition by not working property的主要内容,如果未能解决你的问题,请参考以下文章

mysql窗口函数rank() over、dense_rank() over、row_number() over 使用心得

Oracle-分析函数_总结

oracle的row_number()over rank()over和dense_rank()over这三种分析函数(转)

over在SQL里就啥意思

oracle复杂查询是sql

linq 如何实现sql rank over