Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

SQL Server Self Joins

Posted on 2014-11-19
4
119 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 48

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

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…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

792 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