如何计算Phone
中的重复项?
例如:
State Zip Areacode Phone
A 123 1 989
b 234 2 989
c 345 3 989
d 456 4 123
e 567 5 123
f 678 6 234
g 678 7 234
预期结果:
State Zip Areacode Phone row_count
A 123 1 989 1
b 234 2 989 2
c 345 3 989 3
d 456 4 123 1
e 567 5 123 2
f 678 6 234 1
g 678 7 234 2
答案 0 :(得分:3)
这是一个Window function问题。对于较旧的MySQL版本( <8.0 ),我们可以使用Session variables对其进行仿真。请尝试以下操作:
0 20181010125200000000
Name: Timestamp, dtype: object
PS::OP具有confirmed,SELECT t1.State,
t1.Zip,
t1.Areacode,
@row_count := CASE
WHEN @ph = t1.Phone Then @row_count + 1
ELSE 1
END AS row_count,
@ph := t1.Phone AS Phone
FROM
(SELECT State,
Zip,
Areacode,
Phone
FROM your_table
ORDER BY Phone) AS t1
CROSS JOIN (SELECT @row_count := 1) AS init1
CROSS JOIN (SELECT @ph := '') AS init2
分区中的编号可以是任何数字。
答案 1 :(得分:0)
如果要通过电话号码订购,一个简单的解决方案是:
SELECT State, Zip, Areacode, Phone, `Index` FROM (
SELECT State,
Zip,
Areacode,
Phone,
@Idx := IF(@previous_phone=Phone, IFNULL(@Idx,0)+1,1) as Index,
@previous_phone := Phone
FROM table
ORDER BY Phone
) t;
这将仅保留先前的电话号码并在索引更改时重置索引。
如果您要为组合的“ Zip Area Phone”计算重复次数,则可以根据CONCAT(Zip,Area,Phone)
进行比较,例如
SELECT State, Zip, Areacode, Phone, `Index` FROM (
SELECT State,
Zip,
Areacode,
Phone,
@Idx := IF(@previous_phone=CONCAT(Zip, Area, Phone), IFNULL(@Idx,0)+1,1) as Index,
@previous_phone := CONCAT(Zip, Area, Phone)
FROM table
ORDER BY Zip, Area, Phone
) t;
答案 2 :(得分:0)
下面是通过比较多个列来查找重复记录的MySQL查询
SELECT
Zip, COUNT(Zip),
Areacode, COUNT(Areacode),
Phone, COUNT(Phone)
FROM
TableName
GROUP BY
Zip ,
Areacode ,
Phone
HAVING COUNT(Zip) > 1
AND COUNT(Areacode) > 1
AND COUNT(Phone) > 1;
将TableName替换为Mysql表名称。