wyrd
Well-known member
*cries in frustration*
Okay.. here we go again with paging. I *thought* I had my paging problem solved, unfortunately I came across a new problem. To get fresh I deas I decided to start a new thread and re-explain my problem.
Here we go (buckle your seat belt).
Im keeping track of an Accounting database, very simple. There is a Category table and an Item table. Categories are to categorize items, and Items keep track of the items.
Table: Category
Column: Category_ID (primary key, non clustered)
Column: Name (unique key, clustered index)
Table: Item
Column: Item_ID (primary key, non clustered)
Column: Date --|__ (clustered index date, description)
Column: Description --|
Column: Category_ID (foreign key -> Category.Category_ID)
Column: Cost
Note about Date: I am inserting all dates as the 1st of the month. This program is only keeping track of items purchased on a monthly bases.
When querying for an item, I look for whatever the user specified (by date, category, date & category, or all items). I then order the items by Date, Description (obviously, as thats my clustered index).
Example:
SELECT Item.*, Category.Name FROM Item JOIN Category ON (Item.Category_ID = Category.Category_ID) WHERE Date = @date AND Item.Category_ID = @catid ORDER BY Date, Description
Simple enough? The problem comes into paging. For example I want to display 5 items per page, so to select the first page I just do a simple TOP 5. The next page page I do
SELECT TOP 5 ... WHERE Date > @dateOfLastItem OR (Date >= @dateOfLastItem AND Description >= @desOfLastItem AND Item_ID != @itemid) ORDER BY Date, Description
@dateOfLastItem = The date of the last item on the first page (or previous page, as we keep clicking next)
@desOfLastItem = Description of last item on the first page (or previous page, as we keep clicking next)
All is fine and dandy. This is what I had and it was working.. UNTIL! Notice how my Item.Description is NOT unique? Ohhhhh boy did I set myself up for this one. The simple solution would be "well make Description unique you moron!" Well, boohoo to me, because thats not possible with the way this program was designed. Its designed to keep track of Accounting items, and descriptions may not be unique. IE: Bought some items on 2003/05 from Wal-Mart, itd be entered as Wal-Mart. Now you buy more on 2003/06. Item entered as Wal-Mart.
Where am I going with this? The problem is when several items at the end of the page are of the same date and description. Example data inside my table;
DATE - Description - Category - Cost
03/05 - Wal-Mart - Supplies - $22.50
03/05 - Wal-Mart - Supplies - $12.50
03/05 - Wal-Mart - Supplies - $10.00
03/05 - Wal-Mart - Supplies - $23.23
03/05 - Wal-Mart - Supplies - $72.84
Yes, you read that correct, 5 duplicate items (except the price, but whos to say the price couldnt be the same too?)
So, the current query Im using;
SELECT TOP 5 ... WHERE Date > @dateOfLastItem OR (Date >= @dateOfLastItem AND Description >= @desOfLastItem AND Item_ID != @itemid) ORDER BY Date, Description
Isnt working correctly. As you notice, it checks for Description >= @desOfLastItem AND Item_ID != @itemid. This is good so I dont select the same item on the next page, but the items with the same description just.. I dont know how to explain it. They can re-show on the next page as well, which totally jacks up the paging mechanism. I did a test with 6 items (5 pages per item)..
DATE - Description - Category - Cost
03/05 - Wal-Mart - Supplies - $1.00
03/05 - Wal-Mart - Supplies - $2.00
03/05 - Wal-Mart - Supplies - $3.00
03/05 - Wal-Mart - Supplies - $4.00
03/05 - Wal-Mart - Supplies - $5.00
03/05 - Wal-Mart - Supplies - $6.00
First page comes out fine;
DATE - Description - Category - Cost
03/05 - Wal-Mart - Supplies - $1.00
03/05 - Wal-Mart - Supplies - $2.00
03/05 - Wal-Mart - Supplies - $3.00
03/05 - Wal-Mart - Supplies - $4.00
03/05 - Wal-Mart - Supplies - $5.00
The next page, though (sql statement above), comes out like this;
DATE - Description - Category - Cost
03/05 - Wal-Mart - Supplies - $1.00
03/05 - Wal-Mart - Supplies - $2.00
03/05 - Wal-Mart - Supplies - $3.00
03/05 - Wal-Mart - Supplies - $4.00
03/05 - Wal-Mart - Supplies - $6.00
I thought of one solution, and that was to use either a GROUP BY and calculate the totals as just one Account Item, or to use an INSERT query thatll add the total to another item that has the same date/description instead of adding a new row. HOWEVER, if the person using the program needs to look back on this to make corrections, or to make sure they entered correct data, and had 5 Wal-Mart receipts, theyd have to do calculations on their own to make sure the totals were correct to make sure they entered all of their Wal-Mart costs. Rather if it displayed the duplicate items as it is now (after I fix the paging, if thats possible) they could simply juts eyeball it and go "okay, I entered 5 Wal-Mart purchases for this month" and know it was taken care of.
The other solution I thought of was to simply not allow the user to view All items, and force them to specificy a category and date, that way there wouldnt be to many items displayed (worse case I was thinking 1k items)
So.. any ideas, anyone?
Okay.. here we go again with paging. I *thought* I had my paging problem solved, unfortunately I came across a new problem. To get fresh I deas I decided to start a new thread and re-explain my problem.
Here we go (buckle your seat belt).
Im keeping track of an Accounting database, very simple. There is a Category table and an Item table. Categories are to categorize items, and Items keep track of the items.
Table: Category
Column: Category_ID (primary key, non clustered)
Column: Name (unique key, clustered index)
Table: Item
Column: Item_ID (primary key, non clustered)
Column: Date --|__ (clustered index date, description)
Column: Description --|
Column: Category_ID (foreign key -> Category.Category_ID)
Column: Cost
Note about Date: I am inserting all dates as the 1st of the month. This program is only keeping track of items purchased on a monthly bases.
When querying for an item, I look for whatever the user specified (by date, category, date & category, or all items). I then order the items by Date, Description (obviously, as thats my clustered index).
Example:
SELECT Item.*, Category.Name FROM Item JOIN Category ON (Item.Category_ID = Category.Category_ID) WHERE Date = @date AND Item.Category_ID = @catid ORDER BY Date, Description
Simple enough? The problem comes into paging. For example I want to display 5 items per page, so to select the first page I just do a simple TOP 5. The next page page I do
SELECT TOP 5 ... WHERE Date > @dateOfLastItem OR (Date >= @dateOfLastItem AND Description >= @desOfLastItem AND Item_ID != @itemid) ORDER BY Date, Description
@dateOfLastItem = The date of the last item on the first page (or previous page, as we keep clicking next)
@desOfLastItem = Description of last item on the first page (or previous page, as we keep clicking next)
All is fine and dandy. This is what I had and it was working.. UNTIL! Notice how my Item.Description is NOT unique? Ohhhhh boy did I set myself up for this one. The simple solution would be "well make Description unique you moron!" Well, boohoo to me, because thats not possible with the way this program was designed. Its designed to keep track of Accounting items, and descriptions may not be unique. IE: Bought some items on 2003/05 from Wal-Mart, itd be entered as Wal-Mart. Now you buy more on 2003/06. Item entered as Wal-Mart.
Where am I going with this? The problem is when several items at the end of the page are of the same date and description. Example data inside my table;
DATE - Description - Category - Cost
03/05 - Wal-Mart - Supplies - $22.50
03/05 - Wal-Mart - Supplies - $12.50
03/05 - Wal-Mart - Supplies - $10.00
03/05 - Wal-Mart - Supplies - $23.23
03/05 - Wal-Mart - Supplies - $72.84
Yes, you read that correct, 5 duplicate items (except the price, but whos to say the price couldnt be the same too?)
So, the current query Im using;
SELECT TOP 5 ... WHERE Date > @dateOfLastItem OR (Date >= @dateOfLastItem AND Description >= @desOfLastItem AND Item_ID != @itemid) ORDER BY Date, Description
Isnt working correctly. As you notice, it checks for Description >= @desOfLastItem AND Item_ID != @itemid. This is good so I dont select the same item on the next page, but the items with the same description just.. I dont know how to explain it. They can re-show on the next page as well, which totally jacks up the paging mechanism. I did a test with 6 items (5 pages per item)..
DATE - Description - Category - Cost
03/05 - Wal-Mart - Supplies - $1.00
03/05 - Wal-Mart - Supplies - $2.00
03/05 - Wal-Mart - Supplies - $3.00
03/05 - Wal-Mart - Supplies - $4.00
03/05 - Wal-Mart - Supplies - $5.00
03/05 - Wal-Mart - Supplies - $6.00
First page comes out fine;
DATE - Description - Category - Cost
03/05 - Wal-Mart - Supplies - $1.00
03/05 - Wal-Mart - Supplies - $2.00
03/05 - Wal-Mart - Supplies - $3.00
03/05 - Wal-Mart - Supplies - $4.00
03/05 - Wal-Mart - Supplies - $5.00
The next page, though (sql statement above), comes out like this;
DATE - Description - Category - Cost
03/05 - Wal-Mart - Supplies - $1.00
03/05 - Wal-Mart - Supplies - $2.00
03/05 - Wal-Mart - Supplies - $3.00
03/05 - Wal-Mart - Supplies - $4.00
03/05 - Wal-Mart - Supplies - $6.00
I thought of one solution, and that was to use either a GROUP BY and calculate the totals as just one Account Item, or to use an INSERT query thatll add the total to another item that has the same date/description instead of adding a new row. HOWEVER, if the person using the program needs to look back on this to make corrections, or to make sure they entered correct data, and had 5 Wal-Mart receipts, theyd have to do calculations on their own to make sure the totals were correct to make sure they entered all of their Wal-Mart costs. Rather if it displayed the duplicate items as it is now (after I fix the paging, if thats possible) they could simply juts eyeball it and go "okay, I entered 5 Wal-Mart purchases for this month" and know it was taken care of.
The other solution I thought of was to simply not allow the user to view All items, and force them to specificy a category and date, that way there wouldnt be to many items displayed (worse case I was thinking 1k items)
So.. any ideas, anyone?
Last edited by a moderator: