Solved

JOIN to same table

Posted on 1998-12-09
4
210 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
ID: 1092177

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
ID: 1092178
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
ID: 1092179
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
ID: 1092180
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

863 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

20 Experts available now in Live!

Get 1:1 Help Now