declare @Features table (FeatureID int, ParentID int, Fea...

0

Please login or register to vote for this query.

(click on this box to dismiss)

Gardening and Landscaping Meta

Q&A about the site for gardeners and landscapers

declare @Features table (FeatureID int, ParentID int, Feature varchar(50))
insert @Features values
 (1, 0, 'Apple'),
 (2, 0, 'Boy'), 
 (3, 2, 'Charles'), 
 (4, 1, 'Daddy'), 
 (5, 2, 'Envelope'), 
 (6, 1, 'Frankfurter');

    select  *
    from    @Features feat
    order by
            case 
            when ParentID = 0 
            then FeatureID 
            else    (
                    select  FeatureID 
                    from    @Features parent 
                    where   parent.FeatureID = feat.ParentID
                    ) 
            end
    ,       case when ParentID = 0 then 1 end desc
    ,       FeatureID

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results