Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6784793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:02:33+00:00 2026-05-26T17:02:33+00:00

I am trying to write my SQL Server 2008 query in such a way

  • 0

I am trying to write my SQL Server 2008 query in such a way that I can just loop through my output and output headers as needed. I’ve done this stuff the wrong way many times and had ColdFusion do the hard work within the page, but need this done in SQL Server.

FeatureID ParentID Feature
--------------------------
1         0        Apple      
2         0        Boy 
3         2        Charles
4         1        Daddy
5         2        Envelope
6         1        Frankfurter

I want my query resultset to look like this:

FeatureID ParentID Feature
--------------------------
1         0        Apple      
4         1        Daddy
6         1        Frankfurter
2         0        Boy 
3         2        Charles
5         2        Envelope

If the ParentID is 0, it means that it’s a major category. If the ParentID is greater than 0, it means it’s a minor category, a child of the parent.

So the parents need to be ordered A – Z and the children need to be ordered A-Z.

Can you help me get this ordered correctly?

SELECT FeatureID, ParentID, Feature
FROM Features
ORDER BY
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:02:34+00:00Added an answer on May 26, 2026 at 5:02 pm

    From your comment, if you know there are only two levels, there is an easy solution:

    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
    
    1. Sort by the name of the root element: for the root, this is Feature column. For the children, look up the root’s name with a subquery.
    2. Sort the root on top
    3. Sort the children by name

    Example at SE Data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a query for SQL Server 2005 but I can't
Using SQL Server 2008 Reporting services: I'm trying to write a report that displays
I'm trying to write a recursive query in SQL Server that basically lists a
I'm trying to write an insert statement for a SQL Server table that inserts
I'm trying to write an SQL query that would search within a CSV (or
I am trying to write an aggregate udf for using Sql Server 2008 and
I am trying to write a SQL Server query but have had no luck
I'm trying write a routine in SQL Server that, when run, would traverse specified
I am trying to write a Microsoft SQL Server query for retrieving the oldest
I'm trying to write an asynchronous server that queries a SQL Server database and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.