SQL查询获取重复记录的记录

时间:2017-07-11 12:25:39

标签: sql oracle

我有一张像

这样的表

TAB1

t_no   t_nm   t_ct
 1      abc    NY
 2      pqr    CA
 3      lmn    DC
 3      lmn    DC

如何获取具有重复值的记录,如:

O / P-

t_no   t_nm   t_ct
 3     lmn    DC
 3     lmn    DC

3 个答案:

答案 0 :(得分:2)

您可以使用窗口功能:

select t.*
from (select t.*, count(*) over (partition by t_no, t_nm, t_ct) as cnt
      from t
     ) t
where cnt > 1;

也许算数够了?然后聚合是最简单的:

select t_no, t_nm, t_ct
from t
group by t_no, t_nm, t_ct
having count(*) > 1;

我不确定为什么返回具有相同值的多行很有用。

答案 1 :(得分:0)

这句话也可以解决它:

SELECT data.*
  FROM tbl data
  INNER JOIN 
      (
        SELECT count(*) as rc
              ,t_no
              ,t_nm
              ,t_ct
          FROM tbl
      GROUP BY t_no, t_nm, t_ct
        HAVING COUNT(*) > 1
      ) dupl
    ON data.t_no = dupl.t_no
   AND data.t_nm = dupl.t_nm
   AND data.t_ct = dupl.t_ct

以防您想要删除重复记录并保留其中一个记录:

;WITH cte
AS
(
SELECT *
      ,ROW_NUMBER() OVER (PARTITION BY t_no, t_nm, t_ct ORDER BY t_no, t_nm, t_ct )  AS RowNum
FROM tbl
) 
DELETE FROM cte WHERE RowNum > 1

答案 2 :(得分:0)

function theReplacer(key, value) {
    return key === "amount" ? +value : value;
}