英文:
Need to update id column using row_number()
问题
我有一个包含user_id
和user_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;
然后,在执行MERGE
或UPDATE
之后,表格包含:
USER_ID | USER_NAME | ID |
---|---|---|
123 | ABC | 1 |
456 | BCD | 1 |
123 | ABC | 2 |
123 | ABC | 3 |
234 | XYZ | 1 |
英文:
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 |
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论