0

I have a rather big SQL statement that I am working with in MS Access 2010. Here it goes:

SELECT 
W.ID AS wid,
W.wpt_ty AS ty,
W.wpt_num AS num,
W.wpt_nxt AS nxt,
W.latdeg AS lat,
W.londeg AS lon,
W.alt AS alt,
W.mission_id AS mid,
W.ctg1 AS ctg1,
W.ctg2 AS ctg2,
W.ctg3 AS ctg3,
W.ctg4 AS ctg4,
W.wpt_index AS indx,
W.vel AS vel,
W.tu AS tu,
R.route_num AS rnum,
R.AC_num AS ac,
R.route_type AS rtype,
R.LastUpdatedOn AS d8,
R.LastUpdatedBy AS auth,
R.flight_wpt_count AS wfcount,
M.mission_name AS msnName,
V.Description AS vstatus,
R.disallowed_reason_id AS did,
CW.wpt_num AS c1num,
CR.matching_route_id AS c1mrid,
CW.wpt_index AS c1indx,
CRU.runway_name AS c1rnwy,
CR.route_num AS c1rnum
FROM Validation AS V 
(RIGHT JOIN Runways AS CRU 
 INNER JOIN (Routes CR 
 INNER JOIN Waypoints CW ON CR.ID = CW.route_id) 
 ON Runways.ID = Routes.runway_id
INNER JOIN ((Missions as M 
INNER JOIN Routes AS R ON M.ID = R.mission_id) 
INNER JOIN Waypoints AS W ON (R.ID = W.route_id) 
AND (M.ID = W.mission_id)) ON 
V.ID = R.validated 
WHERE (((R.matching_route_id)=307543) AND ((R.validated) <> 0 ))
AND (((CW.mission_id)=mid) AND ((CW.wpt_num) = (ctg1))))

If you look at the bottom, you can see am I referencing the values ctg1 and mid on a Right Join while the Inner Joins reference other literal values. Eventually I will want to do the same for ctg2, ctg3, and ctg4

Right now I am running these as 2 separate queries but finding it to be way too slow. If I can join combine the queries (sort of like how I am showing here) it could speed things up greatly. But I am at a loss for how to:

  • Using select values earlier in said query from the Inner/Left join and push them into values needed on the Right join.
  • I may be using joins incorrectly, but I thought they had to do with combining data from possible the same tables, just on different pivot points.
  • How to use the MS Access GUI to help write a query like this.
  • I know this is for MS Access but I am tagging for MySQL just in case there are similar queries there which can be ported to MS Access?
E.S.
  • 2,733
  • 6
  • 36
  • 71

1 Answers1

0

Have you tried using UNION for this?

It would allow you to execute this query (As two queries, which you mentioned as a possibility), and join the results for your output.

Be warned, it will eat up (only show one of) your duplicates in the results set.

I also suggest reading up on the different types of joins for your own benefit, in the following answer:

MYSQL Joins

Community
  • 1
  • 1
  • Well the thing about Unions is they just add more rows to the results. I want the values to be added as new columns. – E.S. Mar 31 '15 at 22:53
  • Re-reading this -- how would I join the results after the union? Again, I need to feed the results from the "first" query into the "second" one. I'm not sure how to do that. – E.S. Apr 01 '15 at 18:52