I have a database with a table ( tblPersonnel
) that is populated with following data.
Name_Personnel VesselName SailoutDate Time_transfer Direction
JB Flight 2 3/03/2016 10:38:00 UP
MH Flight 2 3/03/2016 10:38:00 UP
RS Flight 2 3/03/2016 10:38:00 UP
JB Flight 2 3/03/2016 11:40:00 DOWN
MH Flight 2 3/03/2016 11:40:00 DOWN
RS Flight 2 3/03/2016 11:40:00 DOWN
I need to query the total time for all personnel between the "UP" and "DOWN" time. I'd like to come with a output like this.
Name_Personnel VesselName SailoutDate Time_transfer_UP Time_transfer_DOWN Total_time
JB Flight 2 3/03/2016 10:38:00 11:40:00 01:02
MH Flight 2 3/03/2016 10:14:00 11:49:00 01:35
RS Flight 2 3/03/2016 10:36:00 11:53:00 01:17
The Name_personnel
, vesselname
and sailoutdate
always have an "UP" and a "Down" value. So these can be used to search matching rows.
How can I do this?
You can use conditional aggregation. The challenge is the total time. If you can live with total minutes, then it is pretty easy:
select Name_Personnel, VesselName, SailoutDate,
max(iif(direction = 'UP', time_transfer, NULL)) as time_transfer_up,
max(iif(direction = 'DOWN', time_transfer, NULL)) as time_transfer_down,
datediff("minute",
max(iif(direction = 'UP', time_transfer, NULL))
max(iif(direction = 'DOWN', time_transfer, NULL))
) as minutes_diff
from tblPersonnel
group by Name_Personnel, VesselName, SailoutDate;
Thanks, Both answers worked fine, although I saw different output, as not all data was completely correctly filled.
My final query became this.
SELECT DateDiff("n",Max(IIf([direction]='UP',[time_transfer],Null)),Max(IIf([direction]='DOWN',[time_transfer],Null))) AS minutes_diff, tblPersons.Name_Personnel, tblPersons.VesselName, tblPersons.SailoutDate, tblPersons.Type, tblPersons.VesselName, Max(IIf([direction]='up',[time_transfer],Null)) AS Time_transfer_UP, Max(IIf([direction]='Down',[time_transfer],Null)) AS Time_tranfer_DOWN
FROM tblPersons, QRY_Numberofsailingdays
GROUP BY tblPersons.Name_Personnel, tblPersons.SailoutDate, tblPersons.Type, tblPersons.VesselName, tblPersons.VesselName
HAVING (((tblPersons.SailoutDate) Between [Forms]![FRM_Working_Time_Personnel]![TXT_startdate] And [Forms]![FRM_Working_Time_Personnel]![TXT_enddate]));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.