我正在提取数据并希望对具有相同值的行进行编号以具有相同的行号。示例:
TRN Date Product ROW_Number ABC1 2015-06-15 001 1 ABC1 2015-06-15 002 1 ABC1 2015-06-15 003 1 DEF1 2015-06-15 001 2 DEF1 2015-06-15 002 2
具有相同TRN的行必须具有相同的row_number。这有可能吗?如果它不能在脚本中完成,那么可以选择在Deski中进行编号,所以如果有人知道我会非常感激!
答案 0 :(得分:2)
不需要PL / SQL,可以使用纯SQL和窗口函数来完成:
select trn, date, product,
dense_rank() over (order by trn) as rn
from the_table;
答案 1 :(得分:0)
使用DENSE_RANK
分析函数:
Oracle 11g R2架构设置:
CREATE TABLE data ( TRN, "Date", Product ) AS
SELECT 'ABC1', DATE '2015-06-15', '001' FROM DUAL
UNION ALL SELECT 'ABC1', DATE '2015-06-15', '002' FROM DUAL
UNION ALL SELECT 'ABC1', DATE '2015-06-15', '003' FROM DUAL
UNION ALL SELECT 'DEF1', DATE '2015-06-15', '001' FROM DUAL
UNION ALL SELECT 'DEF1', DATE '2015-06-15', '002' FROM DUAL;
查询1 :
SELECT d.*,
DENSE_RANK() OVER ( ORDER BY TRN, "Date" ) AS ROW_NUM
FROM data d
<强> Results 强>:
| TRN | Date | PRODUCT | ROW_NUM |
|------|------------------------|---------|---------|
| ABC1 | June, 15 2015 00:00:00 | 001 | 1 |
| ABC1 | June, 15 2015 00:00:00 | 002 | 1 |
| ABC1 | June, 15 2015 00:00:00 | 003 | 1 |
| DEF1 | June, 15 2015 00:00:00 | 001 | 2 |
| DEF1 | June, 15 2015 00:00:00 | 002 | 2 |