If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > parent child, display as follows - possible with SQL?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-05-04, 06:26
Raisor Raisor is offline
Registered User
 
Join Date: Feb 2004
Posts: 5
parent child, display as follows - possible with SQL?

Hi there, I would like to accomplish the following, I have a table as follows:

cat_id
parent_id
cat_descr


------------------
Table filled as:
1, 0, Root
2, 1, Computer
3, 2, Harddisk
4, 2, Floppy drive

I would like to get a display as of:

Root
Root > Computer
Root > Computer > Harddisk
Root > Computer > Floppy drive

Thanks!
Reply With Quote
  #2 (permalink)  
Old 03-05-04, 08:13
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
yes it's possible

you need three queries in a union
PHP Code:
select l1.cat_descr as category1
     
'>'
     
l2.cat_descr as category2
     
'>'
     
l3.cat_descr as category3   
  from yourtable l1
inner
  join yourtable l2
    on l1
.cat_id l2.parent_id          
inner
  join yourtable l3
    on l2
.cat_id l3.parent_id     
union all    
select l1
.cat_descr
     
'>'
     
l2.cat_descr
     
' '
     
' ' 
  
from yourtable l1
inner
  join yourtable l2
    on l1
.cat_id l2.parent_id          
union all    
select l1
.cat_descr
     
' '
     
' '
     
' '
     
' ' 
  
from yourtable l1
order by 1
,2,3,4,
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 03-05-04, 17:41
chuzhoi chuzhoi is offline
Registered User
 
Join Date: Dec 2002
Posts: 134
In general you have to use recursion (Oracle "connect by", DB2/MS SQL Server Yukon - "with")

DB2 example (I added a limit by level, remove if not needed):

with
yourtable(cat_id, parent_id, cat_descr) as (values (1, 0, 'Root'), (2, 1, 'Computer'), (3, 2, 'Harddisk'), (4, 2, 'Floppy drive'))
, tree(level, id, value) as
(
select 1, cat_id, cast(cat_descr as varchar(100)) from yourtable where parent_id = 0
union all
select t2.level+1, t1.cat_id, t2.value || '>' || t1.cat_descr
from
yourtable t1, tree t2
where t1.parent_id = t2.id
and t2.level < 10
)
select value
from tree
order by level



OUTPUT:

VALUE
---------------------------
Root
Root>Computer
Root>Computer>Harddisk
Root>Computer>Floppy drive
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On