ORACLE SQL: 下一个 ID 结束时重置计数序列
ORACLE SQL: Reseting a counting sequence when ID ends for the next one
如果客户的 ID 结束,我目前正在尝试重置序列
现在,它是这样的:
CustomerID Product PosNr
1 Banana 1
1 Papaya 2
1 Apple 3
2 Laptop 1
2 Keyboard 2
希望大家明白我的意思。
PosNr 应该为另一个客户重置。
我可以在将值插入 table 或以任何其他方式设置类似的东西吗?
它是row_number
具有适当分区的解析函数。
SQL> with test (customerid, product) as
2 (select 1, 'banana' from dual union all
3 select 1, 'papaya' from dual union all
4 select 1, 'apple' from dual union all
5 select 2, 'laptop' from dual union all
6 select 2, 'keyboard' from dual
7 )
8 select customerid, product,
9 row_number() over (partition by customerid order by product) posnr
10 from test
11 /
CUSTOMERID PRODUCT POSNR
---------- -------- ----------
1 apple 1
1 banana 2
1 papaya 3
2 keyboard 1
2 laptop 2
SQL>
如果客户的 ID 结束,我目前正在尝试重置序列 现在,它是这样的:
CustomerID Product PosNr
1 Banana 1
1 Papaya 2
1 Apple 3
2 Laptop 1
2 Keyboard 2
希望大家明白我的意思。 PosNr 应该为另一个客户重置。
我可以在将值插入 table 或以任何其他方式设置类似的东西吗?
它是row_number
具有适当分区的解析函数。
SQL> with test (customerid, product) as
2 (select 1, 'banana' from dual union all
3 select 1, 'papaya' from dual union all
4 select 1, 'apple' from dual union all
5 select 2, 'laptop' from dual union all
6 select 2, 'keyboard' from dual
7 )
8 select customerid, product,
9 row_number() over (partition by customerid order by product) posnr
10 from test
11 /
CUSTOMERID PRODUCT POSNR
---------- -------- ----------
1 apple 1
1 banana 2
1 papaya 3
2 keyboard 1
2 laptop 2
SQL>