I have the following Access 2002 query I’m running through OLE DB in .NET:
SELECT TOP 25
tblClient.ClientCode,
tblRegion.Region
FROM (tblClient LEFT OUTER JOIN
tblRegion ON tblClient.RegionCode = tblRegion.RegionCode)
ORDER BY tblRegion.Region
There are 431 records within tblClient that have RegionCode set to NULL.
For some reason, the query above returns all these 431 records instead of the first 25.
If I change the query to ORDER BY tblClient.Client (the name of the client) like so:
SELECT TOP 25
tblClient.ClientCode,
tblRegion.Region
FROM (tblClient LEFT OUTER JOIN
tblRegion ON tblClient.RegionCode = tblRegion.RegionCode)
ORDER BY tblClient.Client
I get the expected result set of 25 records, showing a mixture of region names and NULL values.
Why is it that ordering by a field retrieved through a LEFT OUTER JOIN will the TOP clause not work?
EDIT: Possible solution
When I also ORDER BY tblClient.Client the query appears to work:
SELECT TOP 25
tblClient.ClientCode,
tblRegion.Region
FROM (tblClient LEFT OUTER JOIN
tblRegion ON tblClient.RegionCode = tblRegion.RegionCode)
ORDER BY tblRegion.Region, tblClient.Client
Since I don’t really mind if I sort by a second field, for now I will do this.
When I also
ORDER BY tblClient.Clientthe query appears to work:Since I don’t really mind if I sort by a second field, for now I will do this.
I’ve updated by question to reflect this as a possible solution.