I'm trying to use to create a hierarchical menu from a SQL data source in asp.net. I'm having trouble ordering my table so that I can easily create the menu in asp.net. There may be a better way to do it if anyone has any ideas...
I currently have a table that looks like this (made up sample data), there are only folders at the root and only folders can have folder Ids:
+-------------------+---------------------+-----------+-----------+
| Name_FolderorItem | Parent_Folder | Id_Folder | Menuplace |
+-------------------+---------------------+-----------+-----------+
| c FOLDER | ROOT | c_FOLDER | 1 |
| d FOLDER | j_FOLDER | d_FOLDER | 2 |
| a FOLDER | ROOT | a_FOLDER | 1 |
| j FOLDER | ROOT | j_FOLDER | 1 |
| f FOLDER | ROOT | f_FOLDER | 1 |
| r FOLDER | f_FOLDER | r_FOLDER | 2 |
| i FOLDER | d_FOLDER | i_FOLDER | 3 |
| a ITEM | j_FOLDER | | 2 |
| d ITEM | c_FOLDER | | 2 |
| z ITEM | f_FOLDER | | 2 |
| r ITEM | d_FOLDER | | 3 |
+-------------------+---------------------+-----------+-----------+
I'm thinking that If I order it to get this which is alphabetical on the first level then alphabetical on each deeper level:
+-------------------+---------------------+-----------+-----------+
| Name_FolderorItem | Parent_Folder | Id_Folder | Menuplace |
+-------------------+---------------------+-----------+-----------+
| a FOLDER | ROOT | a_FOLDER | 1 |
| c FOLDER | ROOT | c_FOLDER | 1 |
| d ITEM | c_FOLDER | | 2 |
| f FOLDER | ROOT | f_FOLDER | 1 |
| r FOLDER | f_FOLDER | r_FOLDER | 2 |
| z ITEM | f_FOLDER | | 2 |
| j FOLDER | ROOT | j_FOLDER | 1 |
| a ITEM | j_FOLDER | | 2 |
| d FOLDER | j_FOLDER | d_FOLDER | 2 |
| i FOLDER | d_FOLDER | i_FOLDER | 3 |
| r ITEM | d_FOLDER | | 3 |
+-------------------+---------------------+-----------+-----------+
Then I can use a listview to get this menu structure:
a FOLDER
c FOLDER
- d ITEM
f FOLDER
- r FOLDER (r FOLDER is located in f folder)
- z ITEM
j FOLDER
- a item
- d FOLDER
- - i FOLDER
- - r ITEM
I can't seem to figure out the required SQL to take a folder and then prioritise what is inside of it instead of the other folders/items on that level.
If you have any ideas on the SQL statements that would allow for this ordering I would appreciate it, thanks in advance
EDIT: Here is the Query that I'm now using, thanks for the help
SELECT *
FROM table
START WITH Parent_Folder LIKE 'ROOT'
CONNECT BY PRIOR Id_Folder LIKE Parent_folder;