Results 1 to 5 of 5
  1. #1
    paultje_bos is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Aug 2016
    Posts
    13

    Find related data in a table

    All,



    I am strugling with a problem. I have a flight table with departure and arrival times, each record is a follow up of the previous record (I'll illustrate it below). For a calculation I need the arrival time of the first leg and the departure time of the second leg, but I don't know how I can create a query that tells to use the times from one and an other record.

    Leg Departure Location Arrival Location Departure Time Arrival Time
    1 A B 12:00 12:30
    2 B C 12:35 12:50
    3 C D 12:59 13:15
    4 D A 13:45 14:00

    I want to calculate the waiting times, so on B it should say 5 min, C, 15 min, D 30 min.

    I tried some small queries to determine the next leg and with this to find the departure time, but this didn't work. Hope you have some ideas!

  2. #2
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    What is the purpose of your database? can you briefly describe what you are doing (outside of the database) and what you want to achieve (with the database). I have a feeling this isn't how you should have it set up.

  3. #3
    NTC is offline VIP
    Windows 7 64bit Access 2013
    Join Date
    Nov 2009
    Posts
    2,392
    I assume that you have 1 more field, for the unique flight ID - that is the same value in each of the four records you display.

    But what you point out is exactly correct. A database has no innate order of records - they can be resorted by any column - so that doing math between separate records is not readily at your finger tips.

    There are a couple ways to do this, which are advanced:

    a. set up a union or cross tab query that puts those values all in one record side-by-side and then it is relatively easy to do calculated fields in a query. When one is not familiar with union or cross tab queries it can be a little challenging to initially set those up - you'll want to research them. If your leg quantity really is 4 or less this is viable but if your real data could involve hundred of legs then this is not a viable approach.

    b. change from query Design View to SQL View and write custom join syntax, along with the calculate value. Very advanced. The essence of the join logic is:
    Where FlightID = FlightID AND Leg = (Leg -1)
    plus you will need to fine tune this to deal with the first leg of each flight

    The only easy solution is if you limit the data set to only 1 flight and there is always 4 legs (even if some are blank) as then you can write the data into fixed fields in the form or report object and then set up math using the field names.

    Hope this helps

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Simple example, this only works because I am assuming your leg numbers are sequential and free of error:

    Look at the query qryFinalQuery

    I left the first step in the query as a separate object (qryLegReference) so you could see what I was doing.

    paultje_bos.zip

  5. #5
    paultje_bos is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Aug 2016
    Posts
    13
    Sorry for the late reply, but thank you for the input. I continued with the option of rpeare, because that was doable for me. Thanks again!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 06-06-2016, 06:05 PM
  2. Add sum of data to a record related in another table
    By Rafegh in forum Database Design
    Replies: 6
    Last Post: 11-14-2014, 02:55 PM
  3. Replies: 1
    Last Post: 07-11-2012, 01:42 PM
  4. Replies: 2
    Last Post: 10-19-2011, 06:47 AM
  5. How to find all queries related to a table
    By shanmugamgsn in forum Queries
    Replies: 9
    Last Post: 10-18-2011, 01:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums