我需要查询有关数据库表中重复值的统计信息。例如,假设我有一个电子邮件字段,并且多行可以具有相同的电子邮件。我知道想知道重复多少次 多少次。换句话说:“908封电子邮件重复10次,1783封电子邮件重复9次”等等。
Repeated # of Emails
10 908
9 1783
我不需要查看实际的电子邮件地址,只需要查看这些统计信息。
知道我有这个查询也检索电子邮件地址:
select email_address,
count(email_address) as NumberOccurrences
from table_user_info
group by email_address
having ( count(email_address) > 1 )
如何对这些结果进行分组?
答案 0 :(得分:2)
带有子查询的聚合COUNT()
也会返回聚合COUNT()
。子查询对每个电子邮件地址进行分组和计数,如abc@example.com - 10
中所示,然后外部查询按子查询返回的重复次数计数和分组,丢弃实际的电子邮件地址。
SELECT
repeated,
COUNT(*) AS numemails
FROM (
SELECT
email,
COUNT(*)
FROM emails
GROUP BY email
) emailcounts
答案 1 :(得分:0)
select email_address,
count(email_address) as NumberOccurrences
from table_user_info
group by email_address
having count(*) > 1