【发布时间】:2019-07-11 21:35:16
【问题描述】:
#greatest-n-per-group 派对的另一个人!
我之前的代码:
select count(*)
from revisions join files on rev_file = file_id
where rev_parent_id like 0
and rev_timestamp between '20011231230000' and '20191231225959'
and file_namespace like 0
and file_is_redirect like 0
问题是,对于某些文件,有多个 rev_parent_id=0 的条目。我只想计算那些具有最早 rev_timestamp 的人,但我尝试使用 SQL select only rows with max value on a column 和 Select Earliest Date and Time from List of Distinct User Sessions 中的答案给我 cca 9 000 和 11 000 000。正确的数字应该是 cca 422 000。也许我没能加入三个表格正确,这是我的尝试之一(有 9 000 个结果):
select count(r1.rev_file)
from revisions r1
left outer join revisions r2 on (r1.rev_file = r2.rev_file
and r1.rev_timestamp < r2.rev_timestamp)
join files on r1.rev_file = file_id
where r2.rev_file is NULL
and r1.rev_parent_id like 0
and r1.rev_timestamp between '20011231230000' and '20191231225959'
and file_namespace like 0
and file_is_redirect like 0
表结构:
files
file_id, file_namespace, file_is_redirect
1234, 0, 0
1235, 3, 1
1236, 3, 0
revisions
rev_file, rev_id, rev_parent_id, rev_timestamp
1234, 19, 16, 20170302061522
1234, 16, 0, 20170302061428
1234, 14, 12, 20170302061422
1234, 12, 0, 20170302061237
1235, 21, 18, 20170302061815
1235, 18, 13, 20170302061501
1235, 13, 8, 20170302061355
1235, 8, 3, 20170302061213
1235, 3, 0, 20170302061002
1236, 6, 0, 20170302061014
file_id = rev_file = 文件的 ID。 file_namespace = 文件的 mimetype,0 是明文。 rev_id = 修订的 id。 rev_parent_id = 父版本的 id。 rev_timestamp = 修订的时间戳
唯一有效的文件是 1234,它已被删除并重新创建,因此它有两个 rev_parent_id=0 条目。仅当较旧的 rev_parent_id=0 修订版介于选定时间之间时,我才想计算文件。
【问题讨论】:
-
更新您的问题,添加相关表架构一个适当的数据样本和预期结果
-
我认为字段名称很明显,但是好的,我编辑了这个问题。有没有添加表格的选项? stackoverflow.com/editing-help中什么都没有
标签: mysql join greatest-n-per-group