我有一个包含3列的数据库表。我想找到所有在未注意到并且整理它们的情况下重复的重复项。
表的结构大致
ID ColumnA ColumnB
0 aaa bbb
1 aaa ccc
2 aaa bbb
3 xxx bbb
那么,由于A列和B列都是组合重复的条目,我的查询会是什么样才能返回0和2列?
标准sql首选,但在SQL 2008服务器上运行
答案 0 :(得分:2)
使用count(*)
作为窗口函数:
select t.*
from (select t.*, count(*) over (partition by columna, columnb) as cnt
from table t
) t
where cnt > 1;
答案 1 :(得分:2)
您可以创建一个对重复行进行分组和计数的查询:
SELECT COUNT(1) , ColumnA , ColumnB
FROM YourTable
GROUP BY ColumnA , ColumnB
HAVING COUNT(1) > 1
然后,您可以将其添加到子查询中以输出包含重复数据的完整行。
以下是基于示例数据的完整可执行示例:
CREATE TABLE #YourTable
([ID] INT, [ColumnA] VARCHAR(3), [ColumnB] VARCHAR(3))
;
INSERT INTO #YourTable
([ID], [ColumnA], [ColumnB])
VALUES
(0, 'aaa', 'bbb'),
(1, 'aaa', 'ccc'),
(2, 'aaa', 'bbb'),
(3, 'xxx', 'bbb')
;
SELECT *
FROM #YourTable t1
WHERE EXISTS ( SELECT COUNT(1) , ColumnA , ColumnB
FROM #YourTable
WHERE t1.ColumnA = ColumnA AND t1.ColumnB = ColumnB
GROUP BY ColumnA , ColumnB
HAVING COUNT(1) > 1 )
DROP TABLE #YourTable
答案 2 :(得分:0)
您可以尝试这样:
with x as (select *,rn = row_number()
over(PARTITION BY columnA,columnB order by ID)
from #temp1)
select * from x where rn > 1
答案 3 :(得分:0)
您可以使用带有HAVING子句的子选择来查找重复的列A列对,然后外部SELECT只返回匹配的行。
select * from MyTable t1
inner join (select ColumnA, ColumnB
from MyTable
group by ColumnA, ColumnB
having count(*) > 1) t2 on t2.ColumnA = t1.ColumnA
and t2.ColumnB = t1.ColumnB
答案 4 :(得分:0)
没有聚合函数的代码:
SELECT
a.*
FROM
#tbl a
JOIN #tbl b ON a.[ColumnA] = b.[ColumnA]
AND a.[ColumnB] = b.[ColumnB]
AND a.id <> b.id
OR
SELECT
a.*
FROM
#tbl a
WHERE
EXISTS ( SELECT
*
FROM
#tbl b
WHERE
a.[ColumnA] = b.[ColumnA]
AND a.[ColumnB] = b.[ColumnB]
AND a.ID <> b.ID )
OR
SELECT * FROM (
SELECT
a.*, COUNT(*) OVER (PARTITION BY [ColumnA], [ColumnB]) cnt
FROM
#tbl a
) a
WHERE cnt > 1
答案 5 :(得分:-1)
这种方法可能会引起争议,许多人可以宣称它是“一种不好的做法”但是,它确实完美地翻译了“从'表中挑选所有重复的东西'” 当然,它也适用于删除语句。
SELECT FROM mytable WHERE Id NOT IN
(SELECT Id FROM
(SELECT Id, concat(ColumnA,'-',ColumnB) AS x FROM mytable
GROUP BY x) AS innerTable);
如果可能(或必要),您偶尔也可以在这些列上添加唯一索引。
ALTER TABLE mytable
ADD CONSTRAINT uniqueColA_ColB UNIQUE (ColumnA,ColumnB);
尝试插入重复值时,Sql会自动抛出错误。