我有一个表,我正试图查询它,如下所示:
t_documents
id
user_id
submitted_date
text
status用户可以在文档表中拥有多个文档,并且可以多次提交相同的文本。我想要一种方法,看看有多少重复提交是每个用户。因此,对于前任:
VALUES (1, 1234, 2016-07-05, "this is a test", 3)
VALUES (2, 1234, 2016-07-06, "this is a test", 3)
VALUES (3, 5678, 2016-07-07, "this is another test", 3)
VALUES (4, 5678, 2016-07-08, "this is another test", 3)对于上面的数据集,我想要的结果是给我一个用户1234的记录,重复的文本和重复文本已经提交的次数。我尝试了以下几点:
select oring.user_id, orig.text, COUNT(1) as dups
from t_documents orig
join t_documents another
on orig.user_id = another.user_id
and orig.text = another.text
group by user_id以上是超级粗糙,不起作用。有人能告诉我怎么做吗?我感兴趣的另一个查询是,在所有用户中总共有多少重复条目?
发布于 2016-10-21 04:57:17
我不确定你是否需要自己加入这里。用户ID和文本列上的简单GROUP BY应该足够了:
SELECT user_id, COUNT(*) AS dup_count
FROM t_documents
GROUP BY user_id, text我在这里假设,在确定文本是否重复时,您不关心发布日期。
编辑:
如果要在所有用户中查找重复项的总数,则可以尝试以下查询:
SELECT SUM(t.dup_count)
FROM
(
SELECT user_id, COUNT(*) - COUNT(DISTINCT text) AS dup_count
FROM t_documents
GROUP BY user_id
) thttps://stackoverflow.com/questions/40168679
复制相似问题