Solved

SQL Server Self Joins

Posted on 2014-11-19
4
120 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 49

Accepted Solution

by:
Vitor Montalvão earned 350 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 150 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

713 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