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
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