Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

JOIN to same table

Posted on 1998-12-09
4
Medium Priority
?
219 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 300 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

886 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