Solved

Creating a query to show all records from one table plus those that are linked from another

Posted on 2016-10-19
3
39 Views
Last Modified: 2016-10-19
I think my requirement should be quite simple, but just cannot seem to figure it out.

I have two related tables - Drivers and Journeys.
I am trying to construct a query that will list ALL drivers and then details of their future [date>=Date()] journeys if they have any.

Maybe like this....
Joe Bloggs
John Smith - Journey1
John Smith - Journey2
Jack Green
Jim Black - Journey3
Fred White - Journey4
Fred White - Journey5
etc etc...

All my attempts so far will only list drivers if they have a journey.

Hope someone can give me a nudge in the right direction.

Regards
Richard
0
Comment
Question by:rltomalin
  • 2
3 Comments
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 41849890
Try something like:

Selecte Drivers.*, J.*
FROM Drivers
LEFT JOIN (
SELECT Journeys.* FROM Journeys
WHERE [Date] > Date()
) as J ON Drivers.DriverID = J.DriverID
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 41849895
to expand on the previous comment,  in order to get all of the drivers, you have to use a LEFT Join to the Journeys table, but once you assign a criteria to the Journeys table, you end up losing all of the drivers who don't have future journeys.  You might also be able to do:

Selecte Drivers.*, Journeys.*
FROM Drivers
LEFT JOIN Journeys ON Drivers.DriverID = Journeys.DriverID
WHERE (Journeys.[Date] IS NULL) OR (Journeys.[Date] > Date())
0
 

Author Closing Comment

by:rltomalin
ID: 41850214
Hi Dale
The first option works fine - thanks very much.
The second option was similar to my attempts, where the journey criteria limited the list to drivers with journeys.

I think I can work with the first option - will have to build on it now.

Regards
Richard
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question