我要过滤重复项并从这些重复项中获取一条记录。(电话号码列仅重复项,其他不重复)
For eg:
row_count state phone areacode zip
1 a 123 989 321
2 z 123 981 322
3 x 123 982 323
4 d 321 983 324
5 a 321 984 325
6 b 098 985 326
7 c 098 986 327
我想要这样的O / P:
row_count state phone areacode zip
1 a 123 989 321
4 d 321 983 324
6 b 098 985 326
答案 0 :(得分:1)
使用相关子查询
select * from table1 t1 where row_count =
(select min(row_count)
from table1 t2
where t1.phone=t2.phone
)
答案 1 :(得分:0)
一种方法联接到子查询,该子查询过滤掉所有记录,但那些电话号码的row_count
最少:
SELECT t1.*
FROM yourTable t1
INNER JOIN
(
SELECT phone, MIN(row_count) AS min_row_count
FROM yourTable
GROUP BY phone
) t2
ON t1.phone = t2.phone AND t1.row_count = t2.min_row_count;
这是使用解析功能的MySQL 8+方法:
SELECT row_count, state, phone, areacode, zip
FROM
(
SELECT *, MIN(row_count) OVER (PARTITION BY phone) min_row_count
FROM yourTable
) t
WHERE row_count = min_row_count;