| Author |
Topic |
|
Phibian
Starting Member
32 Posts |
Posted - 2004-04-28 : 16:57:59
|
| I think this is an easy problem but I just can't seem to figure it out. I was attempting a solution using derived tables.I have (sample data):Table (Menu)Parent_ID, ID, Name0 1 Administration0 2 Options1 3 Admin 11 4 Admin 22 5 Option 12 6 Option 21 7 Admin 3I want:AdministrationAdmin 1Admin 2Admin 3OptionsOption 1Option 2This sorts by Parent ID, but for every root parent (ie parent ID 0), it then sorts by the children with that parent ID before continuing with the next root parent.Any pointers would be wonderful! |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-04-28 : 17:09:21
|
| Does this work for you?DROP TABLE #menuCREATE TABLE #menu( Parent_ID INT, ID INT, Name VARCHAR(55))INSERT #menu( Parent_ID, ID, Name) SELECT 0, 1, 'Administration' UNION ALL SELECT 0, 2, 'Options' UNION ALL SELECT 1, 3, 'Admin 1' UNION ALL SELECT 1, 4, 'Admin 2' UNION ALL SELECT 2, 5, 'Option 1' UNION ALL SELECT 2, 6, 'Option 2' UNION ALL SELECT 1, 7, 'Admin 3'SELECT NameFROM #menuORDER BY CASE Parent_ID WHEN 0 THEN ID ELSE Parent_ID END, IDMeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
Phibian
Starting Member
32 Posts |
Posted - 2004-04-28 : 17:21:33
|
| I didn't know that you could put CASE statements in an ORDER BY, but I'll try it. Thanks!Isn't there another way without using a CASE statement though? |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-04-28 : 17:28:21
|
| There's about a million ways. The CASE is pretty efficient. Remember the ORDER BY gets evaluated and performed after the recordset so the overhead doing it this way can be better than doing a fancy nested join.MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
Phibian
Starting Member
32 Posts |
Posted - 2004-04-28 : 18:47:22
|
| I agree that the CASE is going to be the most efficient.But can anyone suggest one of the other million ways? If I go with the CASE statement, it will be efficient but not portable, and under the circumstances portability is probably more desirable than efficiency since the number of records in the particular table is quite low. |
 |
|
|
vijayakumar_svk
Yak Posting Veteran
50 Posts |
Posted - 2004-04-29 : 06:43:16
|
| Try this one? and check it up this will work for another set of data?select name from menu order by left(name,1),idvijayakumar_svk@hotmail.comWork smarter not harder take control of your life be a super achiever |
 |
|
|
Phibian
Starting Member
32 Posts |
Posted - 2004-04-30 : 12:16:29
|
| That seems to just sort alphabetically. |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-04-30 : 12:45:51
|
| How is it not portable? You will always create the category before the sub-categories. So, I can't think of an instance this would not work. Can you? The other options are to go with nested tables, temp tables, or redesign of the structure to have a nested hierarchy using recursive keys.MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
Phibian
Starting Member
32 Posts |
Posted - 2004-04-30 : 13:28:36
|
| CASE statements aren't portable because they are SQL Server specific and we use a bunch of different databases as well (MS Access, PostGreSQL to name a couple).The category is always created before the sub-category, unless the sub-category originally belongs to a different category and is later moved to a new one. But the CASE statement seems to still work in that case. The issue is really trying to get a solution that works more generally."The other options are to go with nested tables, temp tables, or redesign of the structure to have a nested hierarchy using recursive keys."I'd have to look up nested tables (not something I've done before) but wouldn't I have the same problem using a temp table? In order to get the info into the temp table, I'd have to be able to select the parent and all its children.I could fix the problem simply by adding a column by which to sort and giving the parent and child the same sort number (probably what I'm going to do). But I still have this gnawing feeling at the back of my mind that there is a way to alternate results somehow so that you can subselect all children records in between parents, without needing a CASE.Ah well |
 |
|
|
|