SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help with a query - self join on table


Help with a query - self join on table

Author
Message
winston Smith
winston Smith
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1767 Visits: 2064
Im haveing a lot of trouble writing a query. Ive been spinning my wheels for over an hour, and cant figure out what im not seeing.

Below is my data source. It is one table. Note each object has a distinct objectKey, but not a distinct ObjectID. I want to populate a column called "RootObjectKey. In this column i get the objectKey, based on the RootObjectID joining on the ObjectID (self join).

The problem is that i need it retruned in this format, total of 17 rows. When i self join the table on objectID = RootObjectID 18 rows as objectID is represented twice.

Has anyone any idea how i return the 16 columns, populated wihtthe correct ObjectKey in the RootObjectKey column?

I hope that is clear enough to understand. thanks for any help.



ObjectKey ObjectID RootObjectID RootObjectKey
1 1 1
2 2 1
3 3 1
4 4 1
5 5 1
6 6 1
7 7 1
8 8 1
9 9 9
10 10 10
11 11 11
12 12 12
13 13 13
14 13 13
15 15 15



Cadavre
Cadavre
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3878 Visits: 8472
Hey,

could you read this article, then set-up the DDL and sample data in this format please? Then include the expected results based on your sample data.

Thanks :-D


Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
winston Smith
winston Smith
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1767 Visits: 2064
Hi. Nevermind, i figured that what im trying to do is impossible and i need more info. i think some rows should be versioned, so i need to go to the source data to see if versioning column is available. thanks!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search