Monday, 3 July 2017

Parent child Order By

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 Feature
            else    (
                    select  Feature
                    from    @Features parent
                    where   parent.FeatureID = feat.ParentID
                    )
            end
    ,       case when ParentID = 0 then 1 end desc
    ,       Feature

No comments:

Post a Comment

React Hooks - custom Hook

  v CustomHook Ø React allows us to create our own hook which is known as custom hook. Example – 1 localStorage Demo Step-1 Create ...