Solved

JOIN to same table

Posted on 1998-12-09
4
212 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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 set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

777 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