我正在尝试从访问表中获取每个用户每天的第一笔和最后笔录。用户一天可以进出多次。因此,同一天每个员工都有多个记录。我只想提取第一个和最后一个条目。在访问表中,我的数据如下所示,预期的输出显示在屏幕截图中。
Date Time Terminal ID SL Name Employee ID
------------------------------------------------------------------------------
26-10-19 13:03:21 0103:Device-IN 418 Md.Ariful Islam BRI-000041
26-10-19 8:00:23 0102:Device-In 418 Md.Ariful Islam BRI-000041
26-10-19 7:58:12 0101:Device-Out 418 Md.Ariful Islam BRI-000041
26-10-19 13:05:55 0101:Device-Out 340 Md.Hasan Gazi BRI-000043
26-10-19 9:03:19 0102:Device-In 340 Md.Hasan Gazi BRI-000043
26-10-19 16:12:31 0101:Device-Out 341 Md.Jakir Hossain BRI-000044
26-10-19 14:38:57 0102:Device-In 341 Md.Jakir Hossain BRI-000044
26-10-19 13:29:12 0101:Device-Out 341 Md.Jakir Hossain BRI-000044
26-10-19 9:32:52 0102:Device-In 341 Md.Jakir Hossain BRI-000044
26-10-19 7:34:13 0101:Device-Out 341 Md.Jakir Hossain BRI-000044
26-10-19 13:00:28 0101:Device-Out 335 Md.Nazim Uddin BRI-000045
26-10-19 8:25:42 0102:Device-In 335 Md.Nazim Uddin BRI-000045
26-10-19 15:03:11 0102:Device-In 396 Md.Sojib Khan BRI-000046
26-10-19 14:00:30 0101:Device-Out 288 Md.Rasel Hossain BRI-000048
26-10-19 14:00:26 0101:Device-Out 288 Md.Rasel Hossain BRI-000048
26-10-19 8:29:46 0102:Device-In 288 Md.Rasel Hossain BRI-000048
您可以尝试使用UNION作为最大输入和最小输出
select date
, max(time)
, Terminal_ID
, SL
, Name
, Employee_ID
from my_table
where Terminal_ID like ('%IN')
GROUP BY date, Terminal_ID, SL, Name, Employee_ID
UNION
select date
, min(time)
, Terminal_ID
, SL
, Name
, Employee_ID
from my_table
where Terminal_ID like ('%OUT')
GROUP BY date, Terminal_ID, SL, Name, Employee_ID
order by date, Employee_ID,Terminal_ID