I have a sql statement that involves aliases, the TOP keyword, and inner joins. The statement that produces the first page of my report works like a charm. However, the statement that gets the info for page 2, 3, etc, is doing something odd. I'm supposed to get 50 records (streets) at a time. Somehow I'm getting 26-48, then it skips 49 & 50 and gets 51-75 (like it's supposed to), then I get 76-99, and it skips 100 & 101, then gives me 102-125. So every other page it gives me exactly 25, the other pages it skips records and returns less than 25.
Here is my statement. Forgive me, it's rather long...
SELECT Streets.City, Streets.Name, Streets.Name + ' ' + Streets.Type as Street, ...ETC Many Fields..., Addresses.PIN, ESNs.ESN FROM Streets INNER JOIN (Addresses INNER JOIN (Segments INNER JOIN ESNs ON Segments.ESNId = ESNs.Id) ON Addresses.StreetId = Segments.StreetId) ON Streets.Id = Segments.StreetId WHERE Addresses.StreetId IN (SELECT TOP " & PageSize & " Streets.Id FROM Streets WHERE Streets.City = ? AND Streets.Id NOT IN (SELECT TOP " & (PageSize * (Page - 1)) & " Streets.Id FROM Streets WHERE Streets.City = ? ORDER BY Streets.Name) ORDER BY Streets.Name) AND Addresses.Address BETWEEN Segments.BAddress AND Segments.EAddress ORDER BY Streets.Name, Streets.Type, Addresses.Address
The PageSize is 25, and ? is the town chosen by the user. The statement that gets the first 25 is very similar and works great. Why am I not getting exactly 25 records every time? I would appreciate any assistance...Thanks!
Here is my statement. Forgive me, it's rather long...
SELECT Streets.City, Streets.Name, Streets.Name + ' ' + Streets.Type as Street, ...ETC Many Fields..., Addresses.PIN, ESNs.ESN FROM Streets INNER JOIN (Addresses INNER JOIN (Segments INNER JOIN ESNs ON Segments.ESNId = ESNs.Id) ON Addresses.StreetId = Segments.StreetId) ON Streets.Id = Segments.StreetId WHERE Addresses.StreetId IN (SELECT TOP " & PageSize & " Streets.Id FROM Streets WHERE Streets.City = ? AND Streets.Id NOT IN (SELECT TOP " & (PageSize * (Page - 1)) & " Streets.Id FROM Streets WHERE Streets.City = ? ORDER BY Streets.Name) ORDER BY Streets.Name) AND Addresses.Address BETWEEN Segments.BAddress AND Segments.EAddress ORDER BY Streets.Name, Streets.Type, Addresses.Address
The PageSize is 25, and ? is the town chosen by the user. The statement that gets the first 25 is very similar and works great. Why am I not getting exactly 25 records every time? I would appreciate any assistance...Thanks!