根据最小时间MYSQL获取不同的数据

时间:2019-03-04 01:00:50

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

我在这里有一个示例数据:

enter image description here

这是我想要的输出:

enter image description here

如何根据最短时间获得不同的价值?

这是我尝试的更新

enter image description here

2 个答案:

答案 0 :(得分:0)

用于过滤的相关子查询可能是最简单的解决方案:

select t.*
from t
where t.timestamp = (select min(t2.timestamp)
                     from t t2
                     where t2.id = t.id
                    );

如果想要最早的总体记录,可能需要考虑日期:

select t.*
from t
where (t.date, t.time) in (select t2.date, t2.time
                           from t t2
                           where t2.id = t.id
                           order by t2.date desc, t2.time desc
                          );

或者如果您想在每个日期最早记录:

select t.*
from t
where t.timestamp = (select min(t2.timestamp)
                     from t t2
                     where t2.id = t.id and
                           t2.date = t.date
                    );

答案 1 :(得分:0)

对于MySQL 8.0,窗口函数通常是最有效的处理方式:

SELECT col1, col2, col3, col4, col5
FROM (
    SELECT t.*, ROW_NUMBER() OVER(PARTITION BY col2 ORDER BY col4) rn
    FROM mytable t
) x WHERE rn = 1

在早期版本中,我将对关联子查询使用NOT EXISTS条件:

SELECT *
FROM mytable t
WHERE NOT EXISTS (
    SELECT 1 FROM mytable t1 WHERE t1.col2 = t.col2 AND t1.col4 < t.col4 
)

Demo on DB Fiddle

| col1 | col2 | col3       | col4     | col5       |
| ---- | ---- | ---------- | -------- | ---------- |
| 2    | AAA  | Customer 1 | 07:00:00 | 2019-03-04 |
| 3    | BBB  | Customer 2 | 15:00:00 | 2019-03-04 |

要使其高效执行,您需要在mytable(col2, col4)上建立索引:

CREATE INDEX mytable_idx ON mytable(col2, col4);

如果您有多个具有相同col1col2的记录,则可以使用列c1添加其他条件以避免结果集中出现重复,据我所知该表的主键:

SELECT *
FROM mytable t
WHERE NOT EXISTS (
    SELECT 1 
    FROM mytable t1 
    WHERE 
        t1.col2 = t.col2 
       AND (
           t1.col4 < t.col4 
           OR (t1.col4 = t.col4 AND t1.col1 < t.col1)
       )
)

Updated DB Fiddle