help writing SQL query including Join and Subquery

baitmaster

Well-known Member
Joined
Mar 12, 2009
Messages
2,042
I’m working with SQL in Excel, and it’s working well. Now I want to add a new query that’s slightly more complex than the others and is outside my limited SQL comfort range.

I have 3 tables (worksheets):
  • Table1 contains JourneyRef, RouteNum, DepTime, ArrTime
  • Table 2 contains RouteNum, Origin & Destination
  • Table 3 contains Location1, Location2 and timeTaken

RouteNum is the same in several tables. Location1 equates to Destination and Location2 equates to Origin

I want to find the whole record in table 1 where (DepTime less timeTaken) is the earliest possible but greater than a given time X
I can see I need to do some joins and probably a subquery but I’m unsure the best approach. Can anyone please write a single SQL query that will do this?

Thanks
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I've made some progress and am nearly there but have encountered a couple of SQL error messages. I'm now at:
Code:
SELECT [J!JourneyRef], [J!RouteNum], [J!DepTime], [J!ArrTime], [J!DepTime] - [A!TimeTaken] AS Soonest
FROM (([journey_data$] J
LEFT JOIN [routes$] R
    ON ([J!RouteNum] = [R!RouteNum]))
LEFT JOIN [alternatives$] A
    ON ([A!Location1] = [R!Destination]))
WHERE [J!JourneyRef] is not null
ORDER BY Soonest;

The brackets were added around the FROM /JOIN clauses due to the error "syntax error (missing operator) in query expression..." and then I added some more brackets around the ON clauses due to the error "sql join expression not supported" as suggested by article found here

I'm still getting the error message "sql join expression not supported". Does anybody know why?
 
Upvote 0
I get the same error with the much-simplified
Code:
SELECT [J!*] FROM ([journey_data$] J LEFT JOIN [routes$] R ON ([J!RouteNum] = [R!RouteNum]))
 
Upvote 0

Forum statistics

Threads
1,213,557
Messages
6,114,293
Members
448,564
Latest member
ED38

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top