我正在尝试查找具有重复值的行,但仅基于选定数量的列,而不是单个列或整个行。例如,如果我的表看起来像这样:
ID Address State Name
-------------------------------
0 7 Brown NY John
1 3 Red WX Jane
2 7 Brown WX Ted
3 7 Brown NY Fred
我的问题是:
查找行的地址和州字段与另一行的地址和州字段匹配的行的所有ID。
此查询的答案是:
ID Address State Name
------------------------------
0 7 Brown NY John
3 7 Brown NY Fred
任何想法?
建议: How to select multiple columns values same rows from single table
答案 0 :(得分:39)
尝试以下方法:
SELECT A.*
FROM YourTable A
INNER JOIN (SELECT Address, State
FROM YourTable
GROUP BY Address, State
HAVING COUNT(*) > 1) B
ON A.Address = B.Address AND A.State = B.State
答案 1 :(得分:5)
select *
from #table1
where Addr + St in (select Addr + St as FullAddr
from #table1
group by Addr + St
having count(Addr+St) > 1)