| Author |
Topic |
|
kkmurthy
Starting Member
41 Posts |
Posted - 2009-06-24 : 11:33:02
|
| I am pulling the data from the database table as follows:Query start here:WIth CTE(ID,projectname,ProjectLevelID,ParentID,OrganizationName,path) AS (SELECT ID, casewhen ProjectLevelID=1 then ProjectNamewhen ProjectLevelID=2 then 'LVL2-'+ProjectNamewhen ProjectLevelID=3 then 'LVL3-'+ProjectNameend,ProjectLevelID, parentID,OrganizationName,CAST(ID AS varchar(max))FROM dbo.ProjectWHERE parentID IS NULLand IsDeleted=0UNION ALLSELECT t.ID, casewhen t.ProjectLevelID=1 then t.ProjectNamewhen t.ProjectLevelID=2 then 'LVL2-'+t.ProjectNamewhen t.ProjectLevelID=3 then 'LVL3-'+t.ProjectNameend,t.ProjectlevelID, t.parentID,t.OrganizationName,c.path+','+CAST(t.ID as varchar(10))FROM dbo.Project 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,ProjectlevelIDQuery end here:In the above query what is happening is:the data displayslevel1 project first followed Level 2 project which is correct. The problem I have is when the level 3 projects are displayed The level 3 Project does not follow Level2 immedietely.The sample data currently displayed is as follows:259 Project1 1 NULL dept 259260 LVL2-Proj1 2 259 FCS 259,260289 LVL2-proj2 2 259 FCS 259,289261 LVL3-proj1 3 260 FCS 259,260,261262 LVL3-proj2 3 260 FCS 259,260,262263 LVL3-proj3 3 260 FCS 259,260,263264 LVL3-proj4 3 260 FCS 259,260,264265 LVL3-proj5 3 260 FCS 259,260,265290 LVL3-proj6 3 260 FCS 259,260,290291 LVL3-proj7 3 260 FCS 259,260,291In the above case what I want is :259 Project1 1 NULL dept 259260 LVL2-Proj1 2 259 FCS 259,260261 LVL3-proj1 3 260 FCS 259,260,261262 LVL3-proj2 3 260 FCS 259,260,262263 LVL3-proj3 3 260 FCS 259,260,263264 LVL3-proj4 3 260 FCS 259,260,264265 LVL3-proj5 3 260 FCS 259,260,265290 LVL3-proj6 3 260 FCS 259,260,290291 LVL3-proj7 3 260 FCS 259,260,291289 LVL2-proj2 2 259 FCS 259,289Can some one help me on this please.KKM |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-06-24 : 13:04:49
|
| seems like ORDER BY should be as belowORDER BY LEFT(path,CASE WHEN CHARINDEX(',',path)>0 THEN CHARINDEX(',',path) ELSE LEN(path)+1 END-1)*1,ID |
 |
|
|
kkmurthy
Starting Member
41 Posts |
Posted - 2009-06-24 : 14:15:36
|
| Unfortunately ID's doesn't work since ID s are seq numbers which gets generated as the user enters the data in front end. In this case if we sort it by ID's the data appears as below:259 Project1 1 NULL dept 259260 LVL2-Proj1 2 259 FCS 259,260261 LVL3-proj1 3 260 FCS 259,260,261262 LVL3-proj2 3 260 FCS 259,260,262263 LVL3-proj3 3 260 FCS 259,260,263264 LVL3-proj4 3 260 FCS 259,260,264265 LVL3-proj5 3 260 FCS 259,260,265289 LVL2-proj2 2 259 FCS 259,289290 LVL3-proj6 3 260 FCS 259,260,290291 LVL3-proj7 3 260 FCS 259,260,291As you can see above the level 2 ID 289 appears before 290 and 291Is there any other approach please ? |
 |
|
|
|
|
|