简体   繁体   English

根据相同的行值查找值

[英]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_personnelvesselnamesailoutdate始终具有“ 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.

相关问题 基于同一表上的值在同一行上返回多个值 - Returning multiple values on the same row, based on a value on the same table 根据同一行中的行值逐行更新列值 - Update column value based on row value in the same row, row by row SQL基于同一表中的值在同一行上返回不同的值 - SQL Return different values on the same row, based on a value thats in the same table 根据维度之间的匹配,查找前一行的值并将其插入到同一个 SQL 数据库表中 - Find and insert a value from previous row based on a match between dimensions into the same SQL database table 在表的同一行中查找不匹配的值 - Find mismatches values in the same row of a table 根据不同行中的值从同一列中选择值(如INDEX / MATCH?) - Select Values from Same Column Based on Value in Different Row (Like INDEX/MATCH?) 如何根据一行中的值使一个标识符的所有值都相同? - How can I make all values of one identifier the same based a on value in a row? 在多行中查找同一列中具有相同值的行,而另一列具有不同值 - Find row that has same value in one column over multiple rows while another column has different values 在 SQL 如何比较同一表中两行的值并找到最大值然后 select 是整行 - In SQL how to compare values in two rows in the same table and find the highest value and then select its the entire row 根据同一行中的另一个值从行中选择某个值 - Selecting certain value from row based on another value in same row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM