473,320 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

hierarchy query

Tem
I have a table that looks like this. Each row is a reply or a topic when
IsAReplyTo = 0
I need to write a query that pulls all rows of a hierarchy of a given ID

Table Forum
ID IsAReplyTo
4 0
9 4
27 4
63 27
73 0
82 63

given @ID = 63 it should return ID 4,9,27,82

Im not sure where to go from here.
Select * from Forum where IsAReplyTo = @ID

can it be done with a single query?

Tem

Jan 13 '08 #1
2 1311
If you're using SQL Server 2005, you can use Common Table Expressions (CTEs)
to handle the recursive hierarchy. MS has some info about them at:
http://msdn2.microsoft.com/en-us/library/ms190766.aspx

and http://msdn2.microsoft.com/en-us/library/ms186243.aspx

They're a tad tricky to master, but once you get it down they can be
amazing.

--
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression

"Tem" <te*****@yahoo.comwrote in message
news:uR**************@TK2MSFTNGP04.phx.gbl...
>I have a table that looks like this. Each row is a reply or a topic when
IsAReplyTo = 0
I need to write a query that pulls all rows of a hierarchy of a given ID

Table Forum
ID IsAReplyTo
4 0
9 4
27 4
63 27
73 0
82 63

given @ID = 63 it should return ID 4,9,27,82

Im not sure where to go from here.
Select * from Forum where IsAReplyTo = @ID

can it be done with a single query?

Tem
Jan 13 '08 #2
>can it be done with a single query? <<

Yes. Look up the nested sets model for a quick way to do this without
recursive programming.
Jan 13 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: shilsum | last post by:
I would like to seek your expert opinions on the following problem of mine: I have a table to store employees Here is the partial structure of the table EMPID EMP_POS DATE MGMT
1
by: Scott Simpson | last post by:
I can build portions of DOM trees with jdom and xpathapi and using "new Element()" and "appendChild()" or "addContent()". Which function do I use to get a whole portion of a DOM tree though? For...
0
by: Ed West | last post by:
Hello, I am wondering about best practices for class hierarchies and using ADO.Net, especially the DataSet update/delete commands and the data relations... this needs to package/unpackage to xml...
2
by: BigAbility | last post by:
can i get full class hierarchy of visual studio 2005?? especially, i want to get cotrol class hierarchy of vs.net 2005 help me...
8
by: Bostonasian | last post by:
I don't even know where to begin this as hierarchy is always confusing to construct dynamically in any form(query, xslt,etc). However it is very easy to follow when it's presented. I've done...
7
salesmr
by: salesmr | last post by:
I have a query that works fine in Oracle, but I can't seem to get it to work in PHP. The code is: $query = "SELECT lpad(' ', (level - 1) * 4) || emp_name, level "; $query .= "from emp_table";...
2
salesmr
by: salesmr | last post by:
I have created a query using the connect by prior syntax to get a list of all employees and their hierarchy, but am trying to store this information in a table starting with the employee and storing...
0
petepell
by: petepell | last post by:
Hello all, I am developing an application in VB 2008 that works with a SQL2005 DB to store and manipulate employee data. In one section of the app I want to be able to show a treeview of the...
1
by: sibani sahu | last post by:
Hi, I want to generate the organisational structure of my company.I have a table /viewwhich has the emp_id,manager_id and the level_num.The level_num represents the designation level of the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.