sql insert into table from select without duplicates (need more then a DISTINCT)

前端 未结 5 596
渐次进展
渐次进展 2021-01-31 11:01

I am selecting multiple rows and inserting them into another table. I want to make sure that it doesn\'t already exists in the table I am inserting multiple rows into.

5条回答
  •  Happy的楠姐
    2021-01-31 11:48

    INSERT INTO target_table (col1, col2, col3)
    SELECT DISTINCT st.col1, st.col2, st.col3
    FROM source_table st
    WHERE NOT EXISTS (SELECT 1 
                      FROM target_table t2
                      WHERE t2.col1 = st.col1 
                        AND t2.col2 = st.col2
                        AND t2.col3 = st.col3)
    

    If the distinct should only be on certain columns (e.g. col1, col2) but you need to insert all column, you will probably need some derived table (ANSI SQL):

    INSERT INTO target_table (col1, col2, col3)
    SELECT st.col1, st.col2, st.col3
    FROM ( 
         SELECT col1, 
                col2, 
                col3, 
                row_number() over (partition by col1, col2 order by col1, col2) as rn
         FROM source_table 
    ) st
    WHERE st.rn = 1
    AND NOT EXISTS (SELECT 1 
                    FROM target_table t2
                    WHERE t2.col1 = st.col1 
                      AND t2.col2 = st.col2)
    

提交回复
热议问题