根据条件复制记录

Duplicate a record based on a condition

我想为每个 user_id 复制 step4step3 的记录。 我怎样才能做到这一点?

这是预期的结果:

示例数据如下:

WITH t1 AS (
SELECT 'A' AS interim_id, NULL AS user_id, 'step1' AS step UNION ALL
SELECT 'A' AS interim_id, NULL AS user_id, 'step2' AS step UNION ALL
SELECT 'A' AS interim_id, NULL AS user_id, 'step3' AS step UNION ALL
SELECT NULL AS interim_id, 'B' AS user_id, 'step4' AS step )

您可以使用 union all:

select interim_id, user_id, step
from t1
select interim_id, user_id, 'step3'
from t1
where step = 'step4';

如果要更改 table,则:

insert into t1 (interim_id, user_id, step)
    select interim_id, user_id, 'step3'
    from t1
    where step = 'step4';