Question

I have this select statement:

SELECT d.idcprefix,
         d.orgdept,
         d.idcseq,
         d.subcont, 
         d.actualcomp, 
         COUNT (*) AS "No Duplicates"
    FROM DCS_IDC, DCS_IDC z
   WHERE     D.IDCPREFIX = z.idcprefix
         AND z.orgdept = d.orgdept
         AND z.idcseq = d.idcseq
         and D.SUBCONT is not null
  HAVING COUNT (*) > 1
GROUP BY d.idcprefix,
         d.orgdept,
         d.idcseq,
         d.subcont,
         d.actualcomp
ORDER BY d.idcprefix,
         d.orgdept,
         d.idcseq,
         d.subcont,
         d.actualcomp

and I want to delete the rows that are brought back from this statement, any ideas?

A Delete FROM gives me a ORA-01732: data manipulation operation not legal on this view error because, I believe, I am comparing two tables albeit the same table.

I also tried a delete from tablename where rows exist, but that deleted far more rows than I wanted.

The "duplicates" are based on Subcont. There were records inserted into the database / application using the subcont field, which was meant to be blank, so now I have records which are the same except for one has, for example, A in subcont and it's blank in the other:

idcprefix, subcont, orgdept and idcseq
1          A        ABC         1
1                   ABC         1
2          A        BCD         1
2                   BCD         1

The query shows all the records that have a subcont that are dupliates of the ones that don't and I need to remove the ones with the subcont value.

Help would be much appreciated!

Was it helpful?

Solution

Assuming that (idcprefix, subcont, orgdept, idcseq) is unique, how about the below?

DELETE FROM DCS_IDC
WHERE (idcprefix, subcont, orgdept, idcseq) IN (
  WITH del AS (
  SELECT d.idcprefix,
     d.orgdept,
     d.idcseq,
     d.subcont, 
     d.actualcomp, 
     COUNT (*) AS "No Duplicates"
  FROM DCS_IDC, DCS_IDC z
  WHERE     D.IDCPREFIX = z.idcprefix
     AND z.orgdept = d.orgdept
     AND z.idcseq = d.idcseq
     and D.SUBCONT is not null
  HAVING COUNT (*) > 1
  GROUP BY d.idcprefix,
     d.orgdept,
     d.idcseq,
     d.subcont,
     d.actualcomp
  ORDER BY d.idcprefix,
     d.orgdept,
     d.idcseq,
     d.subcont,
     d.actualcomp
  )
 SELECT idcprefix, subcont, orgdept, idcseq FROM del
)

OTHER TIPS

How about:

BEGIN
  FOR aRow IN (SELECT d.idcprefix,
                      d.orgdept,
                      d.idcseq,
                      COUNT(*) AS DUP_COUNT
                 FROM DCS_IDC d
                 HAVING COUNT(*) > 1
                 GROUP BY d.idcprefix,
                          d.orgdept,
                          d.idcseq)
  LOOP
    DELETE FROM DCS_IDC
      WHERE IDCPREFIX = aRow.IDCPREFIX AND
            ORGDEPT = aRow.ORGDEPT AND
            IDCSEQ = aRow.IDCSEQ AND
            SUBCONT IS NOT NULL;
  END LOOP;
END;

No guarantees. Use at your own risk. Not tested on animals - you'll be first!

Share and enjoy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top