Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Join or Union

Status
Not open for further replies.

vBean

Programmer
Jun 13, 2003
12
0
0
US
I have 3 tables in my database. Like:
Code:
tblExams:
examid      examname
1             Test One
2             Test Two
3             Mid-Term
etc.
tblExamQuestions
id          examid             questionid
1             1                   1
2             2                   2
3             2                   1
4             2                   2
5             3                   2
etc.

tblQuestions
questionid            questiontext
1                         What color is the sky?
2                         How old are you?
3                         Can you answer this?
4                         Is this question usused?
etc...

Then my query would return...
questionid           questiontext                       isused
1                    What color is the sky?             1
2                    How old are you?                   1
3                    Can you answer this?               0
4                    Is this question usused?           0

I need a query to return a list of all questions, regardless of whether they have been used in an exam, but also reutn a field denoting that they have been used. So far I have this SQL but it returns duplicate values...it seems no matter hwo I tweak it I cant get what I want...

Code:
SELECT q.questionid, questiontext, "isused"= 
CASE WHEN e.name IS NULL THEN
    0
ELSE
    1
END
FROM tblQuestions As q
LEFT OUTER JOIN tblExamQuestions As eq ON eq.questionid = q.id
LEFT OUTER JOIN tblExam As e ON e.id = eq.examid
 
add DISTINCT after SELECT

remove the second LEFT OUTER JOIN and perform the test inside the CASE on eq.questionid instead

r937.com | rudy.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top