2016-03-22 23 views
0

我有一個數據庫,其中包含填充了以下數據的表(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 

我需要查詢所有人員在「上」和「下」時間之間的總時間。 我想來一個像這樣的輸出。

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 

Name_personnelvesselnamesailoutdate始終有一個 「UP」 和 「向下」 的價值。所以這些可以用來搜索匹配的行。

我該怎麼做?

+0

你嘗試過什麼嗎?你卡在哪裏? –

回答

0

您可以使用條件聚合。挑戰是總時間。如果你能總分鐘數住的話,那是很容易的:

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; 
0

感謝, 這兩個答案的罰款,但我看到了不同的輸出,因爲不是所有的數據完全填寫正確。

我的最終查詢變成了這個。

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]));