需要使用row_number()更新id列。

huangapple go评论68阅读模式
英文:

Need to update id column using row_number()

问题

我有一个包含user_iduser_name列的表格。我需要添加另一列ID,并且希望按照以下方式填充ID。

User_id User_name ID
123 ABC 1
456 BCD 1
123 ABC 2
123 ABC 3
234 XYZ 1

首先,需要找到唯一的user_id以及它出现的次数,然后根据出现的次数分配从1到n的数字。

我可以使用以下SQL查询使用row_number()来获得输出:

select row_number() over (partition by user_id order by user_id) as seq ,user_id,user_name from test_csv

如何使用上述查询中的seq来更新ID呢?

英文:

I have table with user_id and user_name columns. I have to another column as ID. I want to populate the ID as below.

User_id User_name ID
123 ABC 1
456 BCD 1
123 ABC 2
123 ABC 3
234 XYZ 1

First it needs to find the unique user_id and its number of occurrences and then assign 1 to n number based on the occurrences.

I am able to get the output by using row_number()

select row_number() over (partition by user_id order by user_id) as seq ,user_id,user_name from test_csv

How can I update ID with seq coming out of above query.

答案1

得分: 2

使用MERGE语句并在ROWID伪列上进行关联:

MERGE INTO test_csv dst
USING (
  SELECT row_number() over (partition by user_id order by user_id) as seq 
  FROM   test_csv
) src
ON (src.ROWID = dst.ROWID)
WHEN MATCHED THEN
  UPDATE
  SET id = seq;

或者使用相关联的UPDATE语句:

UPDATE test_csv dst
SET id = (
           SELECT seq
           FROM   (
             SELECT row_number() over (partition by user_id order by user_id) as seq 
             FROM   test_csv
           ) src
           WHERE src.ROWID = dst.ROWID  
         );

对于示例数据:

CREATE TABLE test_csv (User_id, User_name, ID) AS
SELECT 123, 'ABC', CAST(NULL AS NUMBER) FROM DUAL UNION ALL
SELECT 456, 'BCD', NULL FROM DUAL UNION ALL
SELECT 123, 'ABC', NULL FROM DUAL UNION ALL
SELECT 123, 'ABC', NULL FROM DUAL UNION ALL
SELECT 234, 'XYZ', NULL FROM DUAL;

然后,在执行MERGEUPDATE之后,表格包含:

USER_ID USER_NAME ID
123 ABC 1
456 BCD 1
123 ABC 2
123 ABC 3
234 XYZ 1

fiddle

英文:

Use a MERGE statement and correlate on the ROWID pseudo-column:

MERGE INTO test_csv dst
USING (
  SELECT row_number() over (partition by user_id order by user_id) as seq 
  FROM   test_csv
) src
ON (src.ROWID = dst.ROWID)
WHEN MATCHED THEN
  UPDATE
  SET id = seq;

or a correlated UPDATE:

UPDATE test_csv dst
SET id = (
           SELECT seq
           FROM   (
             SELECT row_number() over (partition by user_id order by user_id) as seq 
             FROM   test_csv
           ) src
           WHERE src.ROWID = dst.ROWID  
         );

Which, for the sample data:

CREATE TABLE test_csv (User_id, User_name, ID) AS
SELECT 123, 'ABC', CAST(NULL AS NUMBER) FROM DUAL UNION ALL
SELECT 456, 'BCD', NULL FROM DUAL UNION ALL
SELECT 123, 'ABC', NULL FROM DUAL UNION ALL
SELECT 123, 'ABC', NULL FROM DUAL UNION ALL
SELECT 234, 'XYZ', NULL FROM DUAL;

Then, after the MERGE or UPDATE, the table contains:

USER_ID USER_NAME ID
123 ABC 1
456 BCD 1
123 ABC 2
123 ABC 3
234 XYZ 1

fiddle

huangapple
  • 本文由 发表于 2023年6月27日 18:20:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563884.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定