1

我注意到我的数据库中的一个表包含重复的行。这发生在不同的日期。

当我运行这个查询

 select  ACC_REF, CIRC_TYP, CIRC_CD, count(*) from table

 group by ACC_REF, CIRC_TYP, CIRC_CD

 having count(1)>1

我可以看到重复的行以及它存在的次数(似乎总是 2)。

这些行确实有一个唯一的 id,我认为最好删除具有最新 id 的值

我想选择重复但仅具有最高 id 的数据,以便在删除之前将其移动到另一个表。

有人知道我该怎么做这个选择吗?

非常感谢

4

4 回答 4

1

它只会输出当前表中的唯一值以及您为重复条目指定的条件。

这将允许您从一个 select 语句中执行一步“插入 new_table”。无需删除再插入。

select 
         id
        ,acc_ref
        ,circ_typ
        ,circ_cd
from(
      select
             id
            ,acc_ref
            ,circ_typ
            ,circ_cd
            ,row_number() over ( partition by 
                                             acc_ref
                                            ,circ_typ
                                            ,circ_cd
                                order by id desc
                                ) as flag_multiple_id
      from Table
    ) a
where a.flag_multiple_id = 1 -- or > 1 if you want to see the duplicates
于 2013-11-01T13:24:31.520 回答
0

像这样试试

SELECT t1.* FROM table t1, table t2 WHERE t1.id < t2.id AND t1.ACC_REF = t2.ACC_REF AND t1.CIRC_TYP = t2.CIRC_TYP AND t1.CIRC_CD = t2.CIRC_CD
于 2013-10-31T10:16:50.853 回答
0
select  max(id) as maxid, ACC_REF, CIRC_TYP, CIRC_CD, count(*) 
from table
group by ACC_REF, CIRC_TYP, CIRC_CD
having count(*)>1

编辑:

我认为这在 Sybase 中是有效的,它会找到所有重复项,除了 ID 最低的那个

;with a as
(
select  ID, ACC_REF, CIRC_TYP, CIRC_CD, 
row_number() over (partition by ACC_REF, CIRC_TYP, CIRC_CD order by id) rn, 
from table
)
select ID, ACC_REF, CIRC_TYP, CIRC_CD
from a
where rn > 1
于 2013-10-31T10:24:24.437 回答
0

尝试这样的事情:

SELECT t1.*
FROM YOURTABLE t1
INNER JOIN (
  SELECT max(id) ID,
    ACC_REF,
    CIRC_TYP,
    CIRC_CD
  FROM YOURTABLE 
  GROUP BY ACC_REF,
    CIRC_TYP,
    CIRC_CD
  HAVING COUNT(id) > 1
  ) t2 ON t2.id = t1.id;
于 2013-10-31T10:24:45.820 回答