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.
| Author |
Topic |
|
kkmurthy
Starting Member
41 Posts |
Posted - 2008-12-16 : 08:54:38
|
| Hi I have a data stored in project table with columns:ID Projectname ProjectLevelID parentIDAs the user enters throgh the fron end the example data is as follows:ID prjectname ProjectlevelID parentID1 Program1 1 Null12 project1 2 114 Project2 2 115 Subproject1 3 1217 Subproject2 3 1218 subproject3 3 1220 Subproject12 3 1426 Subproject13 3 1430 Program2 1 NulletcThe question I have is as follows:When I query the table I want to get the data in order of Program -its projects -its subprojects. For example I want get the data in the following order:Program1 Project1 Subproject1 Subproject2 Subproject3 Project2 Subproject12 Subproject13 Program2 and so on Can some one help me please to sort the data in the above order |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-16 : 10:19:05
|
| [code];WIth CTE(ID,prjectname,ProjectLevelID,ParentID,path) AS (SELECT ID, prjectname, ProjectlevelID, parentID,CAST(ID AS varchar(max))FROM TableWHERE parentID IS NULLUNION ALLSELECT t.ID, t.prjectname, t.ProjectlevelID, t.parentID,c.path+','+CAST(t.ID as varchar(10))FROM Table tJOIN CTE cON c.ID=t.ParentID)SELECT *FROM CTEORDER BY LEFT(path,CASE WHEN CHARINDEX(',',path)>0 THEN CHARINDEX(',',path) ELSE LEN(path)+1 END-1)*1, ProjectlevelID,ID[/code] |
 |
|
|
kkmurthy
Starting Member
41 Posts |
Posted - 2008-12-16 : 10:56:18
|
| Thank you very much sir for the help |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-16 : 10:59:35
|
welcome |
 |
|
|
|
|
|
|
|