473,776 Members | 1,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1336
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******** ******@TK2MSFTN GP04.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
6549
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
1263
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 example, suppose I have <foo> <bar name="dopey"> <snafu> Text </snafu> </bar>
0
1611
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 for client/server sending. The class properties pretty much correspond to database tables. This is a pretty long question so thanks in advance for reading and commenting!
2
1983
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
1679
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 breaking a linear doc to 2 levels of hierarchy before, but that case had fixed level(its depth didn't go any further than 2). I have xml doc in attribute oriented format as following : <Root> <Survey ID="1" ControlType="Label" ParentID="1" Text="1....
7
1625
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"; $query .= "CONNECT BY manager_id = prior emp_id "; $query .= "START WITH emp_id = '1234567' "; $result = odbc_do($connection, $query); while(odbc_fetch_into($result,&$fields)) {
2
2410
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 their management information all the way up to our president. For example, Employee ID -> Direct Supervisor ID -> Manager ID -> Senior Manager ID -> Director ID -> VP ID -> Pres ID for each employee. Obviously some will have more levels than...
0
1664
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 hierarchy as a whole showing a nested hierarchy of who reports to who such that when it is first loaded, all you will see is the top guy in the company with a plus next to his name. Opening that will reveal his first level reports and opening those will...
1
4477
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 employee.So I used a query as:_ select concat(REPEAT('-',a.level_num *5),a.employee_name) as Org_Chart from ALL_EMPLOYEE_VIEW A order BY a.level_num ; This does not create a tree structure but simply arranges the employees according to their...
0
9627
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9462
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10119
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8951
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7469
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6721
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5492
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4030
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.