从GROUP BY获取具有MAX和MIN时间的id行

时间:2016-02-21 00:29:17

标签: mysql

我不知道如何获取这些信息。

这是我的查询

SELECT
    CAST(reports.report_creation_datetime AS DATE) AS report_date,
    MIN(CAST(reports.report_creation_datetime AS TIME)) AS work_start, <--- I want to get id of this row
    MAX(CAST(reports.report_creation_datetime AS TIME)) AS work_end    <--- and this
FROM       reports
WHERE      reports.report_creation_datetime >= '2016-02-01'
AND        reports.report_creation_datetime < LAST_DAY('2016-02-01') + INTERVAL 1 DAY
GROUP BY   CAST(reports.report_creation_datetime AS DATE)

示例数据

---reports table
report_id     report_creation_datetime
 ------------------------------------
   82           2016-02-01 07:20:00
   80           2016-02-01 10:30:00
   85           2016-02-01 17:00:00
   88           2016-02-02 08:00:00
   87           2016-02-02 16:00:00
   81           2016-02-03 10:50:00

我想要

---expected result
report_date      work_start      wstart_id     ...
 -------------------------------------------------------
 2016-02-01       07:20:00          82         ...
 2016-02-02       08:00:00          88         ...
 2016-02-03       10:50:00          81         ...
 ...
 2016-02-29       07:30:00          199        ...

我认为像这样的子查询 WHERE日期= MIN(日期) 但这不正确

2 个答案:

答案 0 :(得分:1)

有几种不同的方法可以做到这一点。您可以在两个子查询中使用join aggregation将表重新置于自身。您可以使用user-defined variables

也许最简单的解决方案是2个相关的子查询:

SELECT
    CAST(r.report_creation_datetime AS DATE) AS report_date,
    MIN(CAST(r.report_creation_datetime AS TIME)) AS work_start,
    (SELECT id FROM reports r2 
    WHERE CAST(r.report_creation_datetime AS DATE) = CAST(r2.report_creation_datetime AS DATE) 
    ORDER BY r2.report_creation_datetime 
    LIMIT 1) work_start_id,
    MAX(CAST(reports.report_creation_datetime AS TIME)) AS work_end,
    (SELECT id FROM reports r3 
    WHERE CAST(r.report_creation_datetime AS DATE) = CAST(r3.report_creation_datetime AS DATE) 
    ORDER BY r3.report_creation_datetime DESC
    LIMIT 1) work_end_id
FROM       reports r
WHERE      r.report_creation_datetime >= '2016-02-01'
AND        r.report_creation_datetime < LAST_DAY('2016-02-01') + INTERVAL 1 DAY
GROUP BY   CAST(r.report_creation_datetime AS DATE)

这是另一种应该返回关系的替代方案:

SELECT t.report_date, t.work_start, t.work_end, r1.id minid, r2.id maxid 
FROM (
    SELECT
        CAST(reports.report_creation_datetime AS DATE) AS report_date,
        MIN(CAST(reports.report_creation_datetime AS TIME)) AS work_start,
        MAX(CAST(reports.report_creation_datetime AS TIME)) AS work_end
    FROM       reports
    WHERE      reports.report_creation_datetime >= '2016-02-01'
    AND        reports.report_creation_datetime < LAST_DAY('2016-02-01') + INTERVAL 1 DAY
    GROUP BY   CAST(reports.report_creation_datetime AS DATE)
) t 
    JOIN reports r1 ON t.report_date = CAST(r1.report_creation_datetime AS DATE) AND t.work_start = CAST(r1.report_creation_datetime AS TIME)
    JOIN reports r2 ON t.report_date = CAST(r2.report_creation_datetime AS DATE) AND t.work_end = CAST(r2.report_creation_datetime AS TIME)

答案 1 :(得分:0)

你想要实现这样的目标吗? (未经测试)

SELECT distinct CAST(mainReports.report_creation_datetime AS DATE) AS 
report_date, workStartReports.id as StartId, workEndReports.id as EndId FROM
(SELECT MIN(CAST(report_creation_datetime AS TIME)) as minDate,
MAX(CAST(report_creation_datetime AS TIME)) as maxDate From reports WHERE
report_creation_datetime >= '2016-02-01' AND report_creation_datetime < 
LAST_DAY('2016-02-01') + INTERVAL 1 DAY) as MinMaxDates, reports as 
mainReports, reports as workStartReports, reports as workEndReports WHERE
 mainReports.report_creation_datetime >= '2016-02-01' AND 
mainReports.report_creation_datetime < LAST_DAY('2016-02-01') + INTERVAL 1 DAY 
AND workStartReports.report_creation_datetime = MinMaxDates.minDate AND 
workEndReports.report_creation_datetime = MinMaxDates.maxDate