簡體   English   中英

根據相同的行值查找值

[英]Find value based on same row values

我有一個帶有表( 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”和“ Down”值。 因此,這些可用於搜索匹配的行。

我怎樣才能做到這一點?

您可以使用條件聚合。 挑戰在於總時間。 如果您可以使用總分鍾數,那么這很簡單:

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;

謝謝,盡管我看到了不同的輸出,但兩個答案都很好用,因為並非所有數據都被完全正確地填充。

我的最終查詢是這樣。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM