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

x
?
Solved

SQL Server Self Joins

Posted on 2014-11-19
4
Medium Priority
?
130 Views
Last Modified: 2014-11-20
Hi,

I have a table called Test with columns questionid,text,qid

basically qid also a questionid which is a help text for the main question. questions
which does not have help text will have qid as NULL

now i want to retrive all questionid's with the question text but those questions which has
qid I want that text for qid in the same row . so basically I want the question text and the  qid
text (help text) in the same row for each questionid. for those questions where qid is null should return null. and I dont want the qid questionid's appearing as seperate questions in my result sets.

Example

questinid   text                         qid
101         what is ur name ?     102
102         this is mandatory       null
103         DOB                             null

so my result set should be like below :

questionid   text                        helptext
101          What is ur name ?    this is mandatory
103          DOB                           null



can some one please advise me the best query for this ?

Many Thanks
0
Comment
Question by:gvamsimba
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Vitor Montalvão earned 1400 total points
ID: 40451988
Try this one:
SELECT T1.questinid, T1.text, T2.text helptext
FROM Test T1
LEFT OUTER JOIN Test T2 ON T1.qid=T2.questinid
WHERE NOT EXISTS (SELECT 1 FROM Test T3 WHERE T1.questinid=T3.qid)

Open in new window

0
 
LVL 11

Assisted Solution

by:John_Vidmar
John_Vidmar earned 600 total points
ID: 40453796
Similar solution to Vitor, but eliminates the correlated sub-query:
SELECT	T1.questinid
,	T1.text
,	T2.text helptext
FROM	Test T1
LEFT
JOIN	Test T2			ON	T1.qid = T2.questinid
LEFT
JOIN	Test T3			ON	T1.qid = T3.questinid
WHERE	T3.questinid IS NULL

Open in new window

0
 

Author Comment

by:gvamsimba
ID: 40454687
Hi John , your join condition on t3 is incorrect. the below will work...


SELECT      T1.questinid
,      T1.text
,      T2.text helptext
FROM      Test T1
LEFT
JOIN      Test T2                  ON      T1.qid = T2.questinid
LEFT
JOIN      Test T3                  ON      T1.questinid = T3.qid
WHERE      T3.questinid IS NULL
0
 

Author Closing Comment

by:gvamsimba
ID: 40454691
Excellent
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Integration Management Part 2

879 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