Solved

sql order table with parentID

Posted on 2008-10-21
4
414 Views
Last Modified: 2012-06-21
Hi.

Please look at this question:
http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SQL-Server-2005/Q_23827855.html?cid=748#a22762575

It has been closed, so I cant ass comments to it.

I have a table:
ID   |   Name   |   ParentID
1    |  product1 |  NULL
2    |  product2 |  1
3    |  product3 |  2
4    |  product4 |  3
5    |  product5 |  4
6    |  product6 |  5

And I want to make a sql query who orders the products like etc:
Parent:
First child
Grandchildren
great grandchildren
second child
etc.

This SQL extracts all posts from the table:
;with product_hierarchy as
(select id,parentid,1 lev from products where parentid is null
union all
select p.id,p.parentid,ph.lev+1 from products p
join product_hierarchy ph
on ph.id = p.parentid
)
select * from product_hierarchy
0
Comment
Question by:bongii
  • 2
  • 2
4 Comments
 
LVL 37

Expert Comment

by:momi_sabag
ID: 22767730
try this

with product_hierarchy as
(select id,parentid,1 lev , id as path
from products where parentid is null
union all
select p.id,p.parentid,ph.lev+1 , ph.path+'.'+p.id as path
from products p
join product_hierarchy ph
on ph.id = p.parentid
)
select * from product_hierarchy
order by path
0
 

Author Comment

by:bongii
ID: 22768422
I got this error:

Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value '.' to data type int.
0
 

Author Comment

by:bongii
ID: 22784897
There isen´t any solution to this?
0
 
LVL 37

Accepted Solution

by:
momi_sabag earned 500 total points
ID: 22784914
try this

with product_hierarchy as
(select id,parentid,1 lev , cast(id as varchar(max)) as path
from products where parentid is null
union all
select p.id,p.parentid,ph.lev+1 , ph.path+'.'+ cast(p.id as varchar(max)) as path
from products p
join product_hierarchy ph
on ph.id = p.parentid
)
select * from product_hierarchy
order by path
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

803 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