Solved

JOIN to same table

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server / Update DB? 22 36
Error when creating an UPDATE Trigger in SQL 6 20
Database Integrity 1 49
reccommendations for a free msft sql query manager? 4 27
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

861 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