获取一行中每个日期的最小/最大时间

问题描述 投票:0回答:1

我正在尝试构建一个查询,该查询返回一行中每个日期的最大和最短时间,例如我有这个表:

+---------+------------------------+----------+
|name     |Dates                   |Door      |
+---------+------------------------+----------+
|Maria    |2012-02-14 09:04:45.397 |Garage    |
|Maria    |2012-02-14 12:14:20.997 |Entrance |
|Maria    |2013-02-14 12:20:59.407 |Exit      |
|Maria    |2012-02-13 12:24:20.997 |garage    |
|Eli      |2013-02-13 10:30:59.407 |Entrance  |
|Eli      |2013-02-13 12:30:59.407 |Exit      |
+---------+------------------------+----------+

结果应该是这样的:

+---------+------------------------+-----------------------------+
|name     |Entrance                |Exit                         |
+---------+------------------------+-----------------------------+
|Maria    |2012-02-14 09:04:45.397 |2013-02-14 12:20:59.407      |
|Maria    |2012-02-13 12:14:20.997 |          null               |
|Eli      |2013-02-13 10:30:59.407 |2013-02-13 12:30:59.407      |
+---------+------------------------+-----------------------------+
mysql sql datetime max min
1个回答
0
投票

尝试这个查询

    SELECT name, min(dates) as ENTRANCE, IF(max(dates)=MIN(dates),null,max(dates)) as 'Exit' 
from TEST3 GROUP BY name,day(dates);
© www.soinside.com 2019 - 2024. All rights reserved.