[英]Find value based on same row values
I have a database with a table ( tblPersonnel
) that is populated with following data. 我有一个带有表( tblPersonnel
)的数据库,该表中填充了以下数据。
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. Name_personnel
, vesselname
和sailoutdate
始终具有“ UP”和“ Down”值。 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]));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.