0

我有这个选择语句:

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

我想删除从此语句中带回的行,有什么想法吗?

Delete FROM 给我一个ORA-01732: data manipulation operation not legal on this view错误,因为我相信我正在比较两个表,尽管是同一个表。

我还尝试从存在行的表名中删除,但删除的行比我想要的多得多。

“重复”基于 Subcont。使用 subcont 字段将记录插入到数据库/应用程序中,该字段本来是空白的,所以现在我有相同的记录,除了一个记录,例如 subcont 中的 A 和另一个为空白:

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

该查询显示了所有具有 subcont 的记录,这些记录与没有 subcont 的记录重复,我需要删除具有 subcont 值的记录。

帮助将不胜感激!

4

2 回答 2

0

假设 (idcprefix, subcont, orgdept, idcseq) 是唯一的,那么下面的呢?

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
)
于 2013-07-24T11:30:35.660 回答
0

怎么样:

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;

没有保证。使用风险自负。未在动物身上进行测试 - 你会是第一个!

分享和享受。

于 2013-07-24T12:10:31.820 回答