Select first record in a One-to-Many relation using left join

vanwooten

Board Regular
Joined
Dec 15, 2020
Messages
53
Office Version
  1. 365
Platform
  1. Windows
I have two tables:

membersac :
View attachment 112136
ACtoZIP:
View attachment 112137

Trying to get dataset of membersac.area_code, membersac.AC_Count and ANY 1 zip from ACto Zip. I found the following code but I keep getting "Syntax error in query expression." Any ideas?


SELECT membersac.Area_Code, membersac.AC_Count, ACtoZIP.zip
FROM membersac
LEFT JOIN ACtoZIP
ON ACtoZIP.area_code = membersac.Area_Code
AND ACtoZIP.ID = (SELECT TOP 1 ID
FROM ACtoZIP
WHERE ACtoZIP.area_code = membersac.Area_Code);
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I have two tables:

membersac :
View attachment 112136
ACtoZIP:
View attachment 112137

Trying to get dataset of membersac.area_code, membersac.AC_Count and ANY 1 zip from ACto Zip. I found the following code but I keep getting "Syntax error in query expression." Any ideas?


SELECT membersac.Area_Code, membersac.AC_Count, ACtoZIP.zip
FROM membersac
LEFT JOIN ACtoZIP
ON ACtoZIP.area_code = membersac.Area_Code
AND ACtoZIP.ID = (SELECT TOP 1 ID
FROM ACtoZIP
WHERE ACtoZIP.area_code = membersac.Area_Code);
 

Attachments

  • membersac.png
    membersac.png
    8.8 KB · Views: 18
  • ACtoZIP.png
    ACtoZIP.png
    8 KB · Views: 18
Upvote 0
If you paste that into the sql window, it should show you where?

Other than that, get the subquery working first, then place that as criteria in the design window.
Do you actually need the join?, as the criteria is the same?
 
Upvote 0
This might be your issue?

Code:
AND ACtoZIP.ID =

should be (perhaps)
Code:
WHERE ACtoZIP.ID IN

I always have to play with SQL design, I cannot write anything but simple queries directly. :(
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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