Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Database Design and Application Architecture
 Table design to save sort order

Author  Topic 

kneekill
Yak Posting Veteran

76 Posts

Posted - 2010-11-17 : 10:20:12
Hi All,

I have scenario to design a table to save sort order for a combination made in Table1

Table1

ID
--
1
2
3
4
5
6
7

the combination will be made between the IDs of Table1 like below

1 - 4
1 - 3
1 - 7
2 - 6
2 - 5

So the new table design i have in mind is

MasterID---ChildID---SortNumber
1----------4---------1
1----------3---------2
1----------7---------3
2----------6---------1
2----------5---------2

But the problem is the ChildID sort numbers keeps changing many times and i need to update/delete/insert multiple rows with the new sort numbers for each masterID.

Also i cannot delete the whole combination for a masterID and reinsert new sort numbers, as in future i need extend the sort table beyond the purpose of saving sort numbers.

second table design i have in mind is to save childIDs comma Separated and build sort number dynamically at run time.

MasterID---ChildIDs
1----------4,3,7
2----------6,5

every time there is a change in childid combination i will just update the childids Separated by comma. But this is not good solution due to data integrity and no referential integrity.

I request you to guide me to a better table design for the scenario.If any one of you faced a similar scenario, please let me know the table design for your solution.


RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2010-11-17 : 10:40:48
Why not just have MasterID, ChildID and use ROW_NUMBER() OVER (PARTITION BY MasterID ORDER BY MasterID, ChildID ASC) AS SortNumber in your queries? The overhead is small and no need to worry about when children are added to your result set.

This is assuming you are using SQL 2005 or above.
Go to Top of Page

kneekill
Yak Posting Veteran

76 Posts

Posted - 2010-11-17 : 11:34:50
Hi RickD

Thanks for replying. How will Row_Number() help if only the sort numbers related to childIDs are updated/modified ?. To clarify more sort number in the table is saved/used for some custom functionality in the front end (Custom Sort).

quote:
Originally posted by RickD

Why not just have MasterID, ChildID and use ROW_NUMBER() OVER (PARTITION BY MasterID ORDER BY MasterID, ChildID ASC) AS SortNumber in your queries? The overhead is small and no need to worry about when children are added to your result set.

This is assuming you are using SQL 2005 or above.


Go to Top of Page

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2010-11-17 : 11:53:19
What I am saying is don't save this value to your table, use it in the query on the front end instead. You shouldn't be saving things like this in a table anyway.
Go to Top of Page

kneekill
Yak Posting Veteran

76 Posts

Posted - 2010-11-17 : 12:16:30
Hello RickD, then how can one preserve a custom sort (between different user sessions or states)? if not in the database.

can anyone else please suggest me a design for scenario like this

quote:
Originally posted by RickD

What I am saying is don't save this value to your table, use it in the query on the front end instead. You shouldn't be saving things like this in a table anyway.

Go to Top of Page

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2010-11-18 : 04:26:03
Ok, you didn't mention that this was between sessions. How can it be between sessions if you then want to add things to it the next time you run the query for a particular user?

You either give the user the same results and go with your first idea, or you redo the sort order each time the user logs on and just get them to sort by a certain field rather than a row sort, this way you can just save the field id (which will also help with size of table [depending on No of users]). This is probably a better idea if you are going to be using this table to save other information.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-11-18 : 09:00:11
can you explain how sort order will vary between session? whats the basis of this change?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

kneekill
Yak Posting Veteran

76 Posts

Posted - 2010-11-18 : 13:06:28
hi visakh16, i mean to sort order needs to be preserved between the sessions.

example:
user 1 makes some sort order, the same sort should be available for user 2 and vice versa

quote:
Originally posted by visakh16

can you explain how sort order will vary between session? whats the basis of this change?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

kneekill
Yak Posting Veteran

76 Posts

Posted - 2010-11-18 : 13:07:00
quote:
Originally posted by kneekill

hi visakh16, i mean to say sort order needs to be preserved between the sessions.

example:
user 1 makes some sort order, the same sort should be available for user 2 and vice versa and so on

quote:
Originally posted by visakh16

can you explain how sort order will vary between session? whats the basis of this change?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-11-19 : 10:46:14
so when will sort order change? will it be done manually done by somebody?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

kneekill
Yak Posting Veteran

76 Posts

Posted - 2010-11-19 : 16:43:08
Yes exactly, it will be manually done by click of a button in the UI

quote:
Originally posted by visakh16

so when will sort order change? will it be done manually done by somebody?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page
   

- Advertisement -