Home All Groups Group Topic Archive Search About

Sorting a parent-child list

Author
9 Jun 2006 4:23 AM
Sonny Sablan
tblCat

ID     CatName    ParentID

1        Cat 1          NULL
2        Cat 2          NULL
3        Cat 1.1       1
4        Cat 1.2       1
5        Cat 2.1       2
6        Cat 3          NULL
7        Cat 3.1       6


I want to sort this (Select * from tblCat) like this

1        Cat 1
3        Cat 1.1
4        Cat 1.2
2        Cat 2
5        Cat 2.1
6        Cat 3
7        Cat 3.1

Can I do this in a SQL statement?

Sonny

Author
9 Jun 2006 6:31 AM
jsfromynr
Hi Sonny,

I think if you are entering data like catname = Cat 1, cat 1.1 cat 1.2
etc then a simple query can do this

select * from yourtable order by catname

With Warm regards
Jatinder Singh
http://jatindersingh.blogspot.com

Sonny Sablan wrote:
Show quoteHide quote
> tblCat
>
> ID     CatName    ParentID
>
> 1        Cat 1          NULL
> 2        Cat 2          NULL
> 3        Cat 1.1       1
> 4        Cat 1.2       1
> 5        Cat 2.1       2
> 6        Cat 3          NULL
> 7        Cat 3.1       6
>
>
> I want to sort this (Select * from tblCat) like this
>
> 1        Cat 1
> 3        Cat 1.1
> 4        Cat 1.2
> 2        Cat 2
> 5        Cat 2.1
> 6        Cat 3
> 7        Cat 3.1
>
> Can I do this in a SQL statement?
>
> Sonny
Are all your drivers up to date? click for free checkup

Author
9 Jun 2006 7:04 AM
Sonny Sablan
No, the catnames are not that simple...

The names could be anything, below is just an example. So how would I do it
if the catnames could be be ordered?

Sonny

Show quoteHide quote
"jsfromynr" <jatinder.si***@clovertechnologies.com> wrote in message
news:1149834705.061503.82470@i40g2000cwc.googlegroups.com...
> Hi Sonny,
>
> I think if you are entering data like catname = Cat 1, cat 1.1 cat 1.2
> etc then a simple query can do this
>
> select * from yourtable order by catname
>
> With Warm regards
> Jatinder Singh
> http://jatindersingh.blogspot.com
>
> Sonny Sablan wrote:
>> tblCat
>>
>> ID     CatName    ParentID
>>
>> 1        Cat 1          NULL
>> 2        Cat 2          NULL
>> 3        Cat 1.1       1
>> 4        Cat 1.2       1
>> 5        Cat 2.1       2
>> 6        Cat 3          NULL
>> 7        Cat 3.1       6
>>
>>
>> I want to sort this (Select * from tblCat) like this
>>
>> 1        Cat 1
>> 3        Cat 1.1
>> 4        Cat 1.2
>> 2        Cat 2
>> 5        Cat 2.1
>> 6        Cat 3
>> 7        Cat 3.1
>>
>> Can I do this in a SQL statement?
>>
>> Sonny
>
Author
9 Jun 2006 8:51 AM
Omnibuzz
Well how will I know that cat 1.2 comes after cat 1.1 if the name doesn't
suggest anything?, Both has parent ID 1. Can you give a better explanation on
how it will be ordered?

--
-Omnibuzz (The SQL GC)

http://omnibuzz-sql.blogspot.com/



Show quoteHide quote
"Sonny Sablan" wrote:

> No, the catnames are not that simple...
>
> The names could be anything, below is just an example. So how would I do it
> if the catnames could be be ordered?
>
> Sonny
>
> "jsfromynr" <jatinder.si***@clovertechnologies.com> wrote in message
> news:1149834705.061503.82470@i40g2000cwc.googlegroups.com...
> > Hi Sonny,
> >
> > I think if you are entering data like catname = Cat 1, cat 1.1 cat 1.2
> > etc then a simple query can do this
> >
> > select * from yourtable order by catname
> >
> > With Warm regards
> > Jatinder Singh
> > http://jatindersingh.blogspot.com
> >
> > Sonny Sablan wrote:
> >> tblCat
> >>
> >> ID     CatName    ParentID
> >>
> >> 1        Cat 1          NULL
> >> 2        Cat 2          NULL
> >> 3        Cat 1.1       1
> >> 4        Cat 1.2       1
> >> 5        Cat 2.1       2
> >> 6        Cat 3          NULL
> >> 7        Cat 3.1       6
> >>
> >>
> >> I want to sort this (Select * from tblCat) like this
> >>
> >> 1        Cat 1
> >> 3        Cat 1.1
> >> 4        Cat 1.2
> >> 2        Cat 2
> >> 5        Cat 2.1
> >> 6        Cat 3
> >> 7        Cat 3.1
> >>
> >> Can I do this in a SQL statement?
> >>
> >> Sonny
> >
>
>
>
Author
9 Jun 2006 2:16 PM
Jim Underwood
Are you using SQL 2000 or 2005?

I am assuming that you want to order by ID for each top node (parent is
null) and then display the children of that node, sorted by ID, with each of
their children listed below them, etc?

1
1.1
1.2
1.2.1
1.2.2
1.2.2.1
1.3
1.4
1.4.1
1.4.1.1
1.4.1.2
etc...

I believe you need a recursive query to do this.  SQL 2005 lets you use CTEs
(Common Table Expressions) for recursive SQL, but with 2000 you need to use
a stored procedure.

Show quoteHide quote
"Omnibuzz" <Omnib***@discussions.microsoft.com> wrote in message
news:FE1F4D2C-3D84-44B8-AEAA-CCA4C88D9FE1@microsoft.com...
> Well how will I know that cat 1.2 comes after cat 1.1 if the name doesn't
> suggest anything?, Both has parent ID 1. Can you give a better explanation
on
> how it will be ordered?
>
> --
> -Omnibuzz (The SQL GC)
>
> http://omnibuzz-sql.blogspot.com/
>
>
>
> "Sonny Sablan" wrote:
>
> > No, the catnames are not that simple...
> >
> > The names could be anything, below is just an example. So how would I do
it
> > if the catnames could be be ordered?
> >
> > Sonny
> >
> > "jsfromynr" <jatinder.si***@clovertechnologies.com> wrote in message
> > news:1149834705.061503.82470@i40g2000cwc.googlegroups.com...
> > > Hi Sonny,
> > >
> > > I think if you are entering data like catname = Cat 1, cat 1.1 cat 1.2
> > > etc then a simple query can do this
> > >
> > > select * from yourtable order by catname
> > >
> > > With Warm regards
> > > Jatinder Singh
> > > http://jatindersingh.blogspot.com
> > >
> > > Sonny Sablan wrote:
> > >> tblCat
> > >>
> > >> ID     CatName    ParentID
> > >>
> > >> 1        Cat 1          NULL
> > >> 2        Cat 2          NULL
> > >> 3        Cat 1.1       1
> > >> 4        Cat 1.2       1
> > >> 5        Cat 2.1       2
> > >> 6        Cat 3          NULL
> > >> 7        Cat 3.1       6
> > >>
> > >>
> > >> I want to sort this (Select * from tblCat) like this
> > >>
> > >> 1        Cat 1
> > >> 3        Cat 1.1
> > >> 4        Cat 1.2
> > >> 2        Cat 2
> > >> 5        Cat 2.1
> > >> 6        Cat 3
> > >> 7        Cat 3.1
> > >>
> > >> Can I do this in a SQL statement?
> > >>
> > >> Sonny
> > >
> >
> >
> >
Author
10 Jun 2006 2:46 AM
Omnibuzz
well if its sorted by ID, then its fairly simple.

Select * from tblCat order by coalesce(ParentID,ID)

Won't this work?

--
-Omnibuzz (The SQL GC)

http://omnibuzz-sql.blogspot.com/



Show quoteHide quote
"Jim Underwood" wrote:

> Are you using SQL 2000 or 2005?
>
> I am assuming that you want to order by ID for each top node (parent is
> null) and then display the children of that node, sorted by ID, with each of
> their children listed below them, etc?
>
> 1
> 1.1
> 1.2
> 1.2.1
> 1.2.2
> 1.2.2.1
> 1.3
> 1.4
> 1.4.1
> 1.4.1.1
> 1.4.1.2
> etc...
>
> I believe you need a recursive query to do this.  SQL 2005 lets you use CTEs
> (Common Table Expressions) for recursive SQL, but with 2000 you need to use
> a stored procedure.
>
> "Omnibuzz" <Omnib***@discussions.microsoft.com> wrote in message
> news:FE1F4D2C-3D84-44B8-AEAA-CCA4C88D9FE1@microsoft.com...
> > Well how will I know that cat 1.2 comes after cat 1.1 if the name doesn't
> > suggest anything?, Both has parent ID 1. Can you give a better explanation
> on
> > how it will be ordered?
> >
> > --
> > -Omnibuzz (The SQL GC)
> >
> > http://omnibuzz-sql.blogspot.com/
> >
> >
> >
> > "Sonny Sablan" wrote:
> >
> > > No, the catnames are not that simple...
> > >
> > > The names could be anything, below is just an example. So how would I do
> it
> > > if the catnames could be be ordered?
> > >
> > > Sonny
> > >
> > > "jsfromynr" <jatinder.si***@clovertechnologies.com> wrote in message
> > > news:1149834705.061503.82470@i40g2000cwc.googlegroups.com...
> > > > Hi Sonny,
> > > >
> > > > I think if you are entering data like catname = Cat 1, cat 1.1 cat 1.2
> > > > etc then a simple query can do this
> > > >
> > > > select * from yourtable order by catname
> > > >
> > > > With Warm regards
> > > > Jatinder Singh
> > > > http://jatindersingh.blogspot.com
> > > >
> > > > Sonny Sablan wrote:
> > > >> tblCat
> > > >>
> > > >> ID     CatName    ParentID
> > > >>
> > > >> 1        Cat 1          NULL
> > > >> 2        Cat 2          NULL
> > > >> 3        Cat 1.1       1
> > > >> 4        Cat 1.2       1
> > > >> 5        Cat 2.1       2
> > > >> 6        Cat 3          NULL
> > > >> 7        Cat 3.1       6
> > > >>
> > > >>
> > > >> I want to sort this (Select * from tblCat) like this
> > > >>
> > > >> 1        Cat 1
> > > >> 3        Cat 1.1
> > > >> 4        Cat 1.2
> > > >> 2        Cat 2
> > > >> 5        Cat 2.1
> > > >> 6        Cat 3
> > > >> 7        Cat 3.1
> > > >>
> > > >> Can I do this in a SQL statement?
> > > >>
> > > >> Sonny
> > > >
> > >
> > >
> > >
>
>
>
Author
10 Jun 2006 2:51 AM
Omnibuzz
Small change.. I always miss something :)

Select * from tblCat order by coalesce(ParentID,ID),ID

Sonny, Let me know if this was what you wanted.
--
-Omnibuzz (The SQL GC)

http://omnibuzz-sql.blogspot.com/
Author
12 Jun 2006 8:13 PM
Jim Underwood
This will work great if the tree is only one level deep, but if more levels
are added, I think this will break.

Since the OP did not specify how many levels there may be, or exactly how
the order should work, we can only guess.  The following code shows two
select statements, one which orders by parentId, ID, and one which presents
a different order.  The second one works only because I made the CAT column
match the order I wanted, but it illustrates what the desired results may
be.

I think --CELKO-- is correct that this database design does not lend itself
to tree queries, although CTEs would be able to accomplish it.  Recursive
stored procs, imo, are just a pita and more trouble than they are worth
(although in 2000 the only way to get an employee/manager tree).

create table testtree
(id integer primary key
, cat varchar (10) not null
, parentID integer)
go

insert into testtree(id, parentid, cat)
select 1, NULL, '1' union
select 2, 1, '1.1' union
select 3, 1, '1.2' union
select 4, 1, '1.3' union
select 5, 1, '1.4' union
select 6, 3, '1.2.1' union
select 7, 3, '1.2.2' union
select 8, 5, '1.4.1' union
select 9, 7, '1.2.2.1' union
select 10, 8, '1.4.1.1' union
select 11, 8, '1.4.1.2'
go

select * from testtree
order by coalesce(ParentID,ID),ID

select * from testtree
order by cat

drop table testtree

Show quoteHide quote
"Omnibuzz" <Omnib***@discussions.microsoft.com> wrote in message
news:5B12EC7A-9655-42D3-B6F2-BB28ECA008B9@microsoft.com...
> Small change.. I always miss something :)
>
> Select * from tblCat order by coalesce(ParentID,ID),ID
>
> Sonny, Let me know if this was what you wanted.
> --
> -Omnibuzz (The SQL GC)
>
> http://omnibuzz-sql.blogspot.com/
>
>
Author
10 Jun 2006 12:52 PM
--CELKO--
>> Can I do this in a SQL statement? <<

Get a copy of TREES & HIERARCHIES IN SQL.  Look up the Nested Sets
model.  You can avoid all procedural code, such as recursion and loops,
by  picking the proper table design.  Learn to think in sets and not
linked lists.

Bookmark and Share