Joe Oliphant
Joe Oliphant

Reputation: 79

MS Access Multiple SELECT Query

I have three tables:

Fields

ID | FarmID | FIELD_NUM | Year

Farms

ID | ClientID

Clients

ID | CLIENT_NUM

I have a simple query to get CLIENT_NUM from the Clients table:

SELECT 
  (SELECT CLIENT_NUM FROM Clients 
  WHERE ID = (SELECT ClientID FROM Farms WHERE ID = FarmID)) AS CID
FROM Fields 
WHERE Year = 2014 AND FIELD_NUM = '28-2'

In SQL Server, this works just fine; however, in Microsoft Access it takes forever. Is there a better way to run this query?

Upvotes: 0

Views: 3289

Answers (1)

krivtom
krivtom

Reputation: 24916

Instead of using sub-queries, try using JOIN. Depending on how Access optimizes the query it could help with performance, but also it helps a lot with readability:

SELECT Clients.CLIENT_NUM as CID
FROM (Clients INNER JOIN Farms ON Clients.ID = Farms.ClientID)
    INNER JOIN Fields ON Farms.ID = Fields.FarmID
WHERE Fields.Year = 2014 AND Fields.FIELD_NUM = '28-2'

Upvotes: 2

Related Questions