简体   繁体   English

在MySQL中合并具有最大ID和最小ID的行

[英]Combine rows with max id and min id in MySQL

Hi I have this table that save Entrance Employees 嗨,我有此表保存入职员工

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 

How i can select this result that any row have firstEnterTime , fistExitTime , LastEnterTime, LastExitTime for all date and all Employees 我如何选择任何行都具有针对所有日期和所有员工的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 

You can try below using min() and max() aggregation 您可以在下面尝试使用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

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

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