Solved

SQL Server Self Joins

Posted on 2014-11-19
4
113 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 45

Accepted Solution

by:
Vitor Montalvão earned 350 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Excellent
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now