wyrd
Well-known member
EDIT: Solved, read next post.
Im writing a paging mechanism in a database program, and am having a problem with paging one of my tables. The table has the following columns;
id: int
date: smalldatetime
description: varchar(30)
date and description are a clustered index, id is primary and identity column. In my program Im sorting by date, description in ascending order.
Im coming to a problem because the program is inserting dates like so;
5/1/2003
5/1/2003
6/1/2004
Notice something common? Theyre all on the 1st day of the month. This is as intended, and this is not the problem but directly related to the problem.
Bare with me, Im giving you all this info for a reason, and here is that reason; As I mentioned above, Im adding a paging mechanism to my program where each page contains 100 records. The problem is the date. You see, if on record # 90-110 the date column contains 5/1/2003 for example (which is a common occurance), then I have no way to determine which of those rows would be for the 1st or 2nd page, except by using order by description (luckily this is what I wanted anyway). HOWEVER, that in itself is causing a problem.
Heres an example of the query I first tried
($vardate = date from program of last item in previous page)
($vardesc = description from program of last item in previous page)
($varid = id from program of last item in previous page)
SELECT TOP 100 * FROM items WHERE DATEDIFF(day, $vardate, date) >= 0 AND description >= $vardesc AND id != $varid
At first glance this seemed like itd work easily enough. Unfortunately it doesnt. As you might have guessed, it skips over items with a description that is < $vardesc regardless of date. If I switched it to OR then it would obviously display dates I didnt want as itd match anything from description >= $vardesc. And I cant use WITH TIES either, because there could be 10,000 records for a specific month and year, and that would defeat the purpose of paging.
Ive already figured out how to fix this, unfortunately I dont know how to write the sql statement. I need a way to do this;
SELECT TOP 100 * FROM items WHERE
id != $varid
CASE DATEDIFF(day, $vardate, date) WHEN 0 THEN AND description >= $vardesc
AND DATEDIFF(day, $vardate, date) >= 0
Can anyone help? Any other suggestions?
Thanks in advance.
Im writing a paging mechanism in a database program, and am having a problem with paging one of my tables. The table has the following columns;
id: int
date: smalldatetime
description: varchar(30)
date and description are a clustered index, id is primary and identity column. In my program Im sorting by date, description in ascending order.
Im coming to a problem because the program is inserting dates like so;
5/1/2003
5/1/2003
6/1/2004
Notice something common? Theyre all on the 1st day of the month. This is as intended, and this is not the problem but directly related to the problem.
Bare with me, Im giving you all this info for a reason, and here is that reason; As I mentioned above, Im adding a paging mechanism to my program where each page contains 100 records. The problem is the date. You see, if on record # 90-110 the date column contains 5/1/2003 for example (which is a common occurance), then I have no way to determine which of those rows would be for the 1st or 2nd page, except by using order by description (luckily this is what I wanted anyway). HOWEVER, that in itself is causing a problem.
Heres an example of the query I first tried
($vardate = date from program of last item in previous page)
($vardesc = description from program of last item in previous page)
($varid = id from program of last item in previous page)
SELECT TOP 100 * FROM items WHERE DATEDIFF(day, $vardate, date) >= 0 AND description >= $vardesc AND id != $varid
At first glance this seemed like itd work easily enough. Unfortunately it doesnt. As you might have guessed, it skips over items with a description that is < $vardesc regardless of date. If I switched it to OR then it would obviously display dates I didnt want as itd match anything from description >= $vardesc. And I cant use WITH TIES either, because there could be 10,000 records for a specific month and year, and that would defeat the purpose of paging.
Ive already figured out how to fix this, unfortunately I dont know how to write the sql statement. I need a way to do this;
SELECT TOP 100 * FROM items WHERE
id != $varid
CASE DATEDIFF(day, $vardate, date) WHEN 0 THEN AND description >= $vardesc
AND DATEDIFF(day, $vardate, date) >= 0
Can anyone help? Any other suggestions?
Thanks in advance.
Last edited by a moderator: