I have the following Access 2002 query that I run 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 tblClient in tblClient for which RegionCode set to NULL .
For some reason, the above query returns all these 431 records instead of the first 25.
If I changed the request to ORDER BY tblClient.Client (client name) as follows:
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 won't this sorting by field obtained through the LEFT OUTER JOIN work in the TOP expression?
EDIT: Possible Solution
When I also ORDER BY tblClient.Client a request appears:
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 do not mind if I sort by the second field, while I do it.
source share