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

时间:2015-03-24 20:38:32

标签: oracle11g partition-by oracle-analytics

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 个答案:

答案 0 :(得分:0)

使用以下查询

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

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