简体   繁体   English

从GROUP BY获取一天中最大和最小时间的ID行

[英]Get id row with MAX and MIN time in day from a GROUP BY

I don't have any idea how i can get this information. 我不知道如何获取此信息。

This is my query 这是我的查询

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)

SAMPLE DATA 样本数据

---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

I want to get 我想得到

---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        ...

I think about subquery like this WHERE date = MIN(date) but it is not correct 我考虑这样的子查询WHERE date = MIN(date),但它是不正确的

There are a couple different ways to do this. 有两种不同的方法可以做到这一点。 You can join the table back to itself using aggregation in both subqueries. 您可以join使用表回到自身aggregation在两个子查询。 You could use user-defined variables . 您可以使用user-defined variables

Perhaps the easiest solution is 2 correlated subqueries: 也许最简单的解决方案是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)

Here's an alternative which should return ties as well: 这是一个也应该返回领带的替代方法:

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)

Did you want to achieve something like this? 您想实现这样的目标吗? (not tested) (未测试)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM