如果您想在多列中计算重复项,请使用
group by
:
select ColumnA, ColumnB, ColumnC, count(*) as NumDuplicates
from table
group by ColumnA, ColumnB, ColumnC
如果您只想获取重复的值,则计数大于1。您可以使用having
子句来获取:
select ColumnA, ColumnB, ColumnC, count(*) as NumDuplicates
from table
group by ColumnA, ColumnB, ColumnC
having NumDuplicates > 1
如果你确实想要返回所有重复的行,则将最后一个查询与原始数据连接起来:
select t.*
from table t join
(select ColumnA, ColumnB, ColumnC, count(*) as NumDuplicates
from table
group by ColumnA, ColumnB, ColumnC
having NumDuplicates > 1
) tsum
on t.ColumnA = tsum.ColumnA and t.ColumnB = tsum.ColumnB and t.ColumnC = tsum.ColumnC
假设列值均不为空,此方法将有效。如果有空值,则尝试使用以下方法:
on (t.ColumnA = tsum.ColumnA or t.ColumnA is null and tsum.ColumnA is null) and
(t.ColumnB = tsum.ColumnB or t.ColumnB is null and tsum.ColumnB is null) and
(t.ColumnC = tsum.ColumnC or t.ColumnC is null and tsum.ColumnC is null)
编辑:
如果您有NULL
值,您也可以使用NULL
-safe运算符:
on t.ColumnA <=> tsum.ColumnA and
t.ColumnB <=> tsum.ColumnB and
t.ColumnC <=> tsum.ColumnC
on t.ColumnA <=> tsum.ColumnA and t.ColumnB <=> tsum.ColumnB and t.ColumnC <=> tsum.ColumnC
。 - Ross Smith II