在mysql中合并具有max id和min id的行

omjgkv6w  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(348)

嗨,我有这张table,可以节省入场的员工

IO_Id   Employee_Id   EnterDate    EnterTime    ExitDate    ExitTime
--------------------------------------------------------------------------
1          1          11/20/2018   08:00      11/20/2018     09:00
2          1          11/20/2018   09:10      11/20/2018     09:20
3          1          11/20/2018   09:30      11/20/2018     10:00
4          2          11/20/2018   09:30      11/20/2018     11:00
5          1          11/21/2018   10:00      11/21/2018     11:30

如何选择任何一行的所有日期和所有员工都有firstentertime、fistexittime、lastentertime、lastexittime的结果

IO_Id   Employee_Id   EnterDate    firstEnterTime       fistExitTime  LastEnterTime    LastExitTime
----------------------------------------------------------------------------------------------------------
1          1          11/20/2018         08:00                 09:00         09:30            10:00
4          2          11/20/2018         09:30                 11:00         09:30            11:00
5          1          11/21/2018         10:00                 11:30         10:00            11:30
gjmwrych

gjmwrych1#

您可以在下面尝试使用min()和max()聚合

select min(IO_Id) as IO_Id,Employee_Id,EnterDate as first_EnterDat,
min(EnterTime) as first_EnterTime,min(ExitTime) as fistExitTime,max(EnterTime) as last_EnterTime,max(ExitTime) as lasEtxitTime
from tablename
group by Employee_Id,EnterDate
order by IO_Id

相关问题