Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
tokue
Starting Member
16 Posts |
Posted - 2011-11-13 : 00:14:21
|
I have two tables (questions, answers), I need my select statement to return all questions from the first table (questions) and return number of answers for each question from the second table... I tried to write it but when a question has no answers at all in the second table it will not returned at all, how can I fix it so I will have questionTitle even if it hasn't any answers in the second table, please:SELECT q.questionID, q.questionTitle,numberOfAnswers=COUNT(a.answerID) FROM questions q LEFT JOIN answers a ON q.questionID=a.questionID GROUP BY q.questionID, q.questionTitle |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-11-13 : 02:59:12
|
Not sure why that doesn't work - it should.Are you sure you don't have a where clause?==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
tokue
Starting Member
16 Posts |
Posted - 2011-11-13 : 05:01:13
|
I am so sorry, yes I have a where clause but forgot to write it here, here is my code again:SELECT q.questionID, q.questionTitle,numberOfAnswers=COUNT(a.answerID) FROM questions q LEFT JOIN answers a ON q.questionID=a.questionID WHERE (q.dontShow=0 AND a.dontShow=0) GROUP BY q.questionID, q.questionTitle |
 |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-11-13 : 10:11:47
|
SELECT q.questionID, q.questionTitle,numberOfAnswers=COUNT(a.answerID) FROM questions q LEFT JOIN answers a ON q.questionID=a.questionID AND a.dontShow=0WHERE q.dontShow=0GROUP BY q.questionID, q.questionTitle==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-13 : 12:07:14
|
quote: Originally posted by tokue I am so sorry, yes I have a where clause but forgot to write it here, here is my code again:SELECT q.questionID, q.questionTitle,numberOfAnswers=COUNT(a.answerID) FROM questions q LEFT JOIN answers a ON q.questionID=a.questionID WHERE (q.dontShow=0 AND a.dontShow=0) GROUP BY q.questionID, q.questionTitle
see this to understand why it didnt workhttp://weblogs.sqlteam.com/jeffs/archive/2007/05/14/criteria-on-outer-joined-tables.aspx------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
tokue
Starting Member
16 Posts |
Posted - 2011-11-13 : 17:58:03
|
Thank you for your replies and help, now I understand why it didn't work. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-14 : 00:26:07
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|