Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 11th, 2005, 12:36 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
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...
Reply With Quote
  #2 (permalink)  
Old October 11th, 2005, 12:43 PM
Authorized User
 
Join Date: Sep 2005
Location: , , .
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/
Reply With Quote
  #3 (permalink)  
Old October 11th, 2005, 12:49 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
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...
Reply With Quote
  #4 (permalink)  
Old October 11th, 2005, 02:01 PM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Can you post some sample data and expected results?

Reply With Quote
  #5 (permalink)  
Old October 11th, 2005, 02:13 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
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...
Reply With Quote
  #6 (permalink)  
Old October 11th, 2005, 04:43 PM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
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


Reply With Quote
  #7 (permalink)  
Old October 12th, 2005, 12:43 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
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...
Reply With Quote
  #8 (permalink)  
Old October 12th, 2005, 01:22 PM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Glad to help :)

Jim

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:28 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.