Solved

JOIN to same table

Posted on 1998-12-09
4
208 Views
Last Modified: 2010-03-19
I have table that has several colums but only important ones are ID and Parent_ID.

I'd like to greate JOIN between two tables that returns every ID in table ordered so that after line that has Parent_ID = 0 there comes every single line that has Parent_ID equal ID to one that had Parent_ID = 0 (preferably  sorted by date-field)

So if I've understood this correctly this is "zero or one or  many" relationship to same table.

Everything should come out with single query because it would give me best performance.

I'm using SQL Server 6.5 and Cold Fusion 3.11.

Application is one kind of discussion forum.
0
Comment
Question by:pikala
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
pmteixeira earned 100 total points
Comment Utility

Hi,

try this query:

select a.id, b.id, b.parent_id, b. ...
from My_Table a, My_Table b
where a.parent_id=0
  and (b.id = a.id or
       b.parent_id = a.id)
order by a.id, b. ...


The point here is to join the table with itself (meaning: first part -> master, second part -> master + detail) and then order the result by the id of the first part. Get your data from the second part.


Hope this helps,

PMT
0
 

Author Comment

by:pikala
Comment Utility
Thanks! This didn't completely solve problem but I get the point and
I think I'll find my way home from here on.

I didn't previously realize that "a.parent_id=0" part of it..

(part that didn't work as expected was sorting of data - I could be also
from the fact that I'm developing this system now and database is filled
with data that isn't as correct as it should be)


0
 
LVL 3

Expert Comment

by:cognition
Comment Utility
The above solution will only produce one level of a hierarchy of messages. It would not produce messages with an id of 8 or 4 in the following hierarchy. Each number represents a message in a hierarchy like a newsgroup.

1
    2
        8
    3
        4
    10
5
    9
6
    7

There is a solution in books online for this. Search for "expanding hierarchies"
0
 
LVL 1

Expert Comment

by:pmteixeira
Comment Utility
Yes, cognition is right. The above solution just works for one level of parent-child relationship.

To work with multiple levels, Oracle has a START WITH ... CONNECT BY construction in the SELECT syntax, but I don't know if SQLServer has something like this!

Regards,

PMT
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now