Grouping Records?

Posted on 2005-05-03
Medium Priority
Last Modified: 2010-03-19
I have a table named "products" that stores both parent and child products.  A parent product can be assigned several child products.  The table has a field named "fldParent" that determines the products parent.  If fldParent = 0 then the product is a parent.  Otherwise the record is a child record of the product whose id = fldParent. So, I'd like to be able to group/order a listing of products by its parent and child records.  I'd like to be able to order the parents in alphabetical order (based of fldName), and under each parent, have its child records ordered by their fldName.

For Example:

Product A
   Child 1 of Product A
   Child 2 of Product A
Product B
   Child 1 of Product B
   Child 2 of Product B
   Child 3 of Product B
Product C
   Child 1 of Product C

Can this be done in a sql query?


Question by:ingomar
  • 2
  • 2
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 400 total points
ID: 13921808
If there's only *one* level of parents, it's not too bad:

SELECT CASE WHEN fldParent = 0 THEN fldName ELSE SPACE(3) + fldName END
FROM products
ORDER BY CASE WHEN fldParent = 0 THEN fldName ELSE (SELECT fldName FROM products p2 WHERE p2.fldId = products.fldParent) END,
CASE WHEN fldParent = 0 THEN '' ELSE fldName END

Accepted Solution

dbeneit earned 600 total points
ID: 13923092
Excuse me Scott,

with a big tables that select can be very expensive.
If you use in a subquery, query fields, I think you'll executing a new select for another row.
you can use

SELECT CASE WHEN p.fldParent = 0 THEN p.fldName ELSE SPACE(3) + p2.fldName END
FROM products p left join products p2 on p.fldparent=p2.fldid
ORDER BY  CASE WHEN p.fldParent = 0 THEN p.fldName ELSE p2.fldName END, case when p.fldParent = 0 THEN '' ELSE p.fldName END

The good answer is yours.  
LVL 70

Expert Comment

by:Scott Pletcher
ID: 13926444
Quite true in general.  I was expecting this table would be keyed on fldId (or whatever the main key name is), so the subquery would require only an index lookup, not a full scan.  Still, the LEFT OUTER JOIN might perform better and is worth trying.

Author Comment

ID: 13926464
Thanks for your comments. I havent had a chance to try them out yet, but will shortly.

Thanks again,


Author Comment

ID: 13971496
That did the trick.  thanks for you help.


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question