Ordering hierarchy from recursive query results in SQL 2005

后端 未结 4 611
星月不相逢
星月不相逢 2020-12-16 06:36

I\'ve got a \'Task\' table with the following columns (the TaskOrder is for ordering the children within the scope of the parent, not the entire table):

TaskId
Pa         


        
4条回答
  •  抹茶落季
    2020-12-16 07:17

    You don't need all that union stuff, I think this should work:

    select
     TaskId,
     ParentTaskId,
     [Name],
     COALESCE(ParentTaskId, TaskId) as groupField
    from
     task
    order by
     COALESCE(ParentTaskId, TaskId), ParentTaskId, TaskId
    

提交回复
热议问题