列出不同大小写的重复项 SQL 服务器
List different case duplicates SQL Server
我对数据库很陌生,我正在尝试获取具有不同大小写值的重复值列表,例如在我的数据库中
id | email
---------------------
1 | first@email.com
2 | First@email.com
3 | second@mail.com
4 | third@email.com
我可以 select 具有
的重复值
SELECT count(*) as 'duplicates', lower(email) as 'email'
FROM my_table
GROUP BY lower(email)
HAVING count(*) > 1;
这个returns:
duplicates | email
------------------------
2 | first@email.com
Bu 我也想 return 两个不同大小写的电子邮件,例如:
duplicates | email | email2
-----------------------------------------
2 | first@email.com | First@email.com
注意:从初始查询中我可以看出重复项不超过 2 个(这可能会使查询更简单)
提前致谢!
编辑:它在 Microsoft SQL Server 2012
我会建议 array_agg()
这样你就可以得到所有的:
SELECT count(*) as duplicates, lower(email) as email,
ARRAY_AGG(DISTINCT email) as all_emails
FROM my_table
GROUP BY lower(email)
HAVING count(*) > 1;
Here 是一个 db<>fiddle.
在SQL服务器中,可以使用STRING_AGG()
:
SELECT count(*) as duplicates, lower(email) as email,
STRING_AGG(email, ',') as all_emails
FROM my_table
GROUP BY lower(email)
HAVING count(*) > 1;
很遗憾,DISTINCT
不受支持。
如果您提前知道您的副本不超过两个,您可以很容易地做到:
select Min(email) Email, Max(email) Email2
from t
group by lower(email)
having Count(*)=2
我对数据库很陌生,我正在尝试获取具有不同大小写值的重复值列表,例如在我的数据库中
id | email
---------------------
1 | first@email.com
2 | First@email.com
3 | second@mail.com
4 | third@email.com
我可以 select 具有
的重复值 SELECT count(*) as 'duplicates', lower(email) as 'email'
FROM my_table
GROUP BY lower(email)
HAVING count(*) > 1;
这个returns:
duplicates | email
------------------------
2 | first@email.com
Bu 我也想 return 两个不同大小写的电子邮件,例如:
duplicates | email | email2
-----------------------------------------
2 | first@email.com | First@email.com
注意:从初始查询中我可以看出重复项不超过 2 个(这可能会使查询更简单)
提前致谢!
编辑:它在 Microsoft SQL Server 2012
我会建议 array_agg()
这样你就可以得到所有的:
SELECT count(*) as duplicates, lower(email) as email,
ARRAY_AGG(DISTINCT email) as all_emails
FROM my_table
GROUP BY lower(email)
HAVING count(*) > 1;
Here 是一个 db<>fiddle.
在SQL服务器中,可以使用STRING_AGG()
:
SELECT count(*) as duplicates, lower(email) as email,
STRING_AGG(email, ',') as all_emails
FROM my_table
GROUP BY lower(email)
HAVING count(*) > 1;
很遗憾,DISTINCT
不受支持。
如果您提前知道您的副本不超过两个,您可以很容易地做到:
select Min(email) Email, Max(email) Email2
from t
group by lower(email)
having Count(*)=2