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

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
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,994
Messages
6,175,849
Members
452,675
Latest member
duongtruc1610

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