Wrox Programmer Forums
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 11th, 2005, 12:36 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 100
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to ~Bean~
Default Join Help

I have 3 tables in my database. The tables are:

tblExams:
examid
examname
etc...

tblExamQuestions
id
examid
questionid

tblQuestions
questionid
questiontext
etc...

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.poolid
-------------------------
Beware of programmers with screwdrivers...
__________________
-------------------------
Beware of programmers with screwdrivers...
 
Old October 11th, 2005, 12:43 PM
Authorized User
 
Join Date: Sep 2005
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

have you tried distinct or grouping???

“I sense many useless updates in you... Useless updates lead to defragmentation... Defragmentation leads to downtime...Downtime leads to suffering..Defragmentation is the path to the darkside.. DBCC INDEXDEFRAG and DBCC DBREINDEX are the force...May the force be with you" -- http://sqlservercode.blogspot.com/
 
Old October 11th, 2005, 12:49 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 100
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to ~Bean~
Default

I tried both but I think they failed because of the column "isused" I am trying to ouput...

I think the Grouping complains about Grouping by a text/ntext value and Distinct still created 2 rows, one for isused=0 (false) and one for isused=1(true)

any suggestions are appreciated!

-------------------------
Beware of programmers with screwdrivers...
 
Old October 11th, 2005, 02:01 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Can you post some sample data and expected results?

 
Old October 11th, 2005, 02:13 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 100
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to ~Bean~
Default

Sure...

sample data:
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



-------------------------
Beware of programmers with screwdrivers...
 
Old October 11th, 2005, 04:43 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

   The reason for the duplicates is because the per your example, you can have the same question on many exams. So you will get duplicates. Ex: For question 1, you will get 2 rows, for question 2 you will get 3 rows.
   I have not found a slick way to do it in one sql statement but the code below does work. If you have any questions, let me know:

SELECT q.questionid, questiontext, isused = 1
From tblquestions q
where q.questionid in (select distinct questionid
                       from tblExamQuestions)

Union All

SELECT q.questionid, questiontext, isused = 0
From tblquestions q
where q.questionid NOT in (select distinct questionid
                       from tblExamQuestions)


Jim


 
Old October 12th, 2005, 12:43 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 100
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to ~Bean~
Default

thanks Jim one small modification and that worked nicely

-------------------------
Beware of programmers with screwdrivers...
 
Old October 12th, 2005, 01:22 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Glad to help :)

Jim






Similar Threads
Thread Thread Starter Forum Replies Last Post
join dhara_adh SQL Server 2000 2 January 2nd, 2007 12:39 AM
Join or not join madhukp SQL Server 2000 3 February 2nd, 2005 01:11 AM
More than one join jaywhy13 Classic ASP Basics 2 January 31st, 2005 11:38 PM
INNER JOIN msmagied Classic ASP Databases 3 August 29th, 2004 12:28 PM
Oracle 8i inner join and left join problem puteri_84 Oracle 2 August 19th, 2004 07:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.