So I currently have a an issue where I am encountering duplicates based off a composite key when trying to insert into a table from a materialized view. The composite key is composed of three columns: TIMESTAMP, NAME and TYPE. I've tried using the following method but I still run into the same issue (I get a duplicate error message and the stored procedure stops running). IS there a way to handle the exception so that it just skips over the duplicate row? If not I thought of the below DELETE statement to get rid of duplicates, is this the correct approach?
Below is the 'bulk insert' statement that I am using:
DELETE FROM table
where timestamp IN (SELECT DISTINCT ts from mv_1)
and person IN (SELECT distinct name from mv_1)
and TYPE IN (SELECT distinct data_type from mv_1);
commit;
--
DELETE FROM table
WHERE timestamp || person || TYPE IN
(SELECT DISTINCT
ts
|| name
|| data_type
FROM edb.mv_1);
COMMIT;
--
INSERT INTO table
(person
timestamp,
TYPE)
SELECT name,
ts,
data_type
FROM edb.mv_1 a
WHERE a.row_id NOT IN
(SELECT row_id
FROM (SELECT v.*,
ROW_NUMBER ()
OVER (
PARTITION BY v.name,
v.data_type,
v.ts
ORDER BY row_id)
AS rn
FROM mv_1 v) t
WHERE rn > 1)
Note: row_id is a column I made in MV_1 just so I would have a column that would give me a unique identifier.
Thanks in advance!