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


how do i get the column values?


how do i get the column values?

Author
Message
jowee
jowee
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 75
I have this table called parent table.
Id | LastName | FirstName | Gender | ParentID
1 | Jones | Bob | M | NULL have no parent
2 | Allen | Larry | M | NULL have no parent
3 | Martins | Mary | F | NULL have no parent
4 | Martins | Charles | M | 3 parent name MARY
5 | Martins | David | M | 3 parent name MARY
6 | Martins | Shirley | F | 3 parent name MARY
7 | Martins | Noxy | F | 6 parent name SHIRLEY


I need a query that can select the ID, Lastname, FirstName, Gender with parentname using the parentID
Jason-299789
Jason-299789
SSCarpal Tunnel
SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)

Group: General Forum Members
Points: 4917 Visits: 3232
If its a single level then


Select
*
From Parent P1
LEFT JOIN Parent P2 on P1.Id=P2.ParentId



If its a multi level then you will need to lookup a Recursive CTE http://msdn.microsoft.com/en-us/library/ms186243(v=sql.105).aspx

_________________________________________________________________________
SSC Guide to Posting and Best Practices
Cadavre
Cadavre
SSCrazy Eights
SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)

Group: General Forum Members
Points: 8910 Visits: 8490
Firstly, when you ask a question it is polite to provide readily consumable sample data like this: -
--==CONDITIONALLY DROP THE SAMPLE DATA TABLE==--
IF object_id('tempdb..#yourSampleData') IS NOT NULL
BEGIN
DROP TABLE #yourSampleData;
END;
--==FILL THE SAMPLE DATA TABLE WITH THE DATA THAT YOU HAVE SPECIFIED==--
SELECT Id, LastName, FirstName, Gender, ParentID
INTO #yourSampleData
FROM (VALUES(1,'Jones','Bob','M',NULL),(2,'Allen','Larry','M',NULL),
(3,'Martins','Mary','F',NULL),(4,'Martins','Charles','M',3),
(5,'Martins','David','M',3),(6,'Martins','Shirley','F',3),
(7,'Martins','Noxy','F',6)
)a(Id, LastName, FirstName, Gender, ParentID);



That way, anyone wanting to help you can do so with minimal effort.

As for your particular question - I'm sure that whoever is teaching you will have talked about joins. This is an OUTER join.

You could do it in this way: -
SELECT main.Id, main.LastName, main.FirstName, main.Gender,
ISNULL('parent name ' + outerA.FirstName,'have no parent')
FROM #yourSampleData main
OUTER APPLY (SELECT innerQ.FirstName
FROM #yourSampleData innerQ
WHERE innerQ.Id = main.ParentID) outerA;



Or this: -
SELECT main.Id, main.LastName, main.FirstName, main.Gender,
ISNULL('parent name ' + outerA.FirstName,'have no parent')
FROM #yourSampleData main
LEFT OUTER JOIN #yourSampleData outerA ON outerA.Id = main.ParentID;



Both of which return this: -
Id          LastName FirstName Gender 
----------- -------- --------- ------ -------------------
1 Jones Bob M have no parent
2 Allen Larry M have no parent
3 Martins Mary F have no parent
4 Martins Charles M parent name Mary
5 Martins David M parent name Mary
6 Martins Shirley F parent name Mary
7 Martins Noxy F parent name Shirley


You may want to reconsider the design of your table, as each "Id" can have only one parent in your model.


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
jowee
jowee
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 75
Thank you so much!
Please pardon my ignorance.
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