Solved

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

Posted on 2016-10-19
3
25 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now