每组最大N,基于mysql查询中的最近日期

时间:2018-11-30 20:07:32

标签: mysql sql subquery greatest-n-per-group

我需要某种帮助来解决问题,下面是带有扫描数据的表,我希望我的sql查询仅显示具有给定项目的最新report_created记录的记录。一个项目可以有多个scan_id

下表8888_super_start中的示例仅应以最近的report_created时间显示一次,并且应仅显示最新的已扫描文件数。

+--------------------+------------------+---------------+---------------------+---------------------+
| scan_id            | project_name     | files_scanned |  scan_submitted     |  report_created     |
+--------------------+------------------+---------------+---------------------+---------------------+
| ba2468dd-2e38-478a | 8888_super_start | 123           | 2018-01-23 12:58:43 | 2018-01-23 13:48:46 |
| 5d3cb423-4cbb-4196 | 9111_kick_start  | 1040          | 2018-01-23 14:57:15 | 2018-01-23 15:58:33 |
| 75ff4cfd-172a-4f2d | 8888_super_start | 180           | 2018-05-25 14:37:33 | 2018-05-25 20:17:19 |
+--------------------+------------------+---------------+---------------------+---------------------+

我尝试了in the post中提到的方法,但未提供正确的结果。请提供一些建议。

尝试

select sd.scan_id,sd.project_name,sd.files_scanned from blackbox.tb_cx_scan_details sd
left outer join blackbox.tb_cx_scan_details t2
on sd.scan_id = t2.scan_id
and (sd.report_created < t2.report_created
or (sd.report_created = t2.report_created ))
where t2.scan_id is NULL

2 个答案:

答案 0 :(得分:2)

在MySQL 5.x中,您可以使用子查询:

select * from tb_cx_scan_details
  where (project_name, report_created) in (
    select project_name, max(report_created) 
    from tb_cx_scan_details
    group by project_name
  )

在MySQL 8.x中,您可以使用窗口函数:

select *,
    row_number() over (partition by project_name 
                       order by report_created desc) as my_rank
  from tb_cx_scan_details
  where my_rank = 1

答案 1 :(得分:1)

一种方法是先按project_name分组,然后为每个分组获取最新的report_created,然后可以在足够的列上加入此新表,以获取每个项目的其余信息

SELECT
    sd.scan_id,
    sd.project_name,
    sd.files_scanned
FROM
    blackbox.tb_cx_scan_details AS sd
INNER JOIN
    ( SELECT
          project_name, MAX(report_created) AS MaxReportCreated
      FROM
          blackbox.tb_cx_scan_details
      GROUP BY
          project_name ) AS latest ON latest.project_name = sd.project_name
                                   AND latest.MaxReportCreated = sd.report_created