| Author |
Topic |
|
Apples
Posting Yak Master
146 Posts |
Posted - 2008-07-31 : 14:16:03
|
| Here's my schema:---------------------------------tblMembers---------------------------------MemberID | ChoiceID------------------------------------------------------------------tblChoices---------------------------------ChoiceID | ChoiceName---------------------------------I want to output a list of all the choices, and then the number of members who selected each choice. Here's my query:SELECT C.ChoiceName, COUNT(M.ChoiceID)FROM tblMembers M INNER JOIN tblChoices C ON M.ChoiceID = C.ChoiceIDGROUP BY C.ChoiceNameThe only problem is that if no members selected a certain choice, that choice doesn't show up in the list; I'd like it to show up with a 0 next to it. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
Apples
Posting Yak Master
146 Posts |
Posted - 2008-07-31 : 14:28:50
|
| RIGHT OUTER JOIN worked, thank you! |
 |
|
|
Apples
Posting Yak Master
146 Posts |
Posted - 2008-07-31 : 14:40:19
|
| Actually, I have another problem...I just added a where clause to the query, and it stopped showing all the choices. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
Apples
Posting Yak Master
146 Posts |
Posted - 2008-07-31 : 14:55:24
|
| ---------------------------------tblMembers---------------------------------MemberID | ChoiceID | JoinDate------------------------------------------------------------------tblChoices---------------------------------ChoiceID | ChoiceName | Order--------------------------------- @Month int, @Year intASBEGIN SET NOCOUNT ON; SELECT C.ChoiceName, COUNT(M.ChoiceID) FROM tblMembers M INNER JOIN tblChoices C ON M.ChoiceID = C.ChoiceID WHERE (MONTH(M.JoinDate) = @Month) AND (YEAR(M.JoinDate) = @Year) GROUP BY C.ChoiceName, C.Order ORDER BY C.Order ASCEND |
 |
|
|
Apples
Posting Yak Master
146 Posts |
Posted - 2008-07-31 : 15:09:14
|
| Or maybe it's not the WHERE clause at all, could it have something to do with the fact that I added something to the GROUP BY clause? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
Apples
Posting Yak Master
146 Posts |
Posted - 2008-07-31 : 15:55:23
|
| Returns:Dog 11Cat 2Fish 2Giraffe 11Bear 1Lion 20Should return:Dog 11Cat 2Mouse 0Fish 2Whale 0Bird 0Giraffe 11Bear 1Frog 0Lion 20 |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
Apples
Posting Yak Master
146 Posts |
Posted - 2008-07-31 : 16:07:34
|
quote: Originally posted by tkizer Why aren't you using an OUTER JOIN like I said in my first post?Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog
Oops, sorry, I copied that from my original post and forgot to change it back. Here's my new one:@Month int,@Year intASBEGINSET NOCOUNT ON;SELECT C.ChoiceName, COUNT(M.ChoiceID)FROM tblMembers M RIGHT OUTER JOIN tblChoices CON M.ChoiceID = C.ChoiceIDWHERE (MONTH(M.JoinDate) = @Month) AND(YEAR(M.JoinDate) = @Year)GROUP BY C.ChoiceName, C.OrderORDER BY C.Order ASCEND |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
Apples
Posting Yak Master
146 Posts |
Posted - 2008-07-31 : 16:21:30
|
quote: Originally posted by tkizer Why do you have C.Order in the GROUP BY?Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog
Because if I don't, it gives me an error saying:Column "tblChoices.Order" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-07-31 : 16:23:10
|
| Don't just add columns to the GROUP BY to get rid of an error. It will not produce the correct results as you are seeing.So if you removed the column from the GROUP BY, does it produce the correct results (although unsorted to your liking)?Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
Apples
Posting Yak Master
146 Posts |
Posted - 2008-07-31 : 16:26:43
|
quote: Originally posted by tkizer Don't just add columns to the GROUP BY to get rid of an error. It will not produce the correct results as you are seeing.So if you removed the column from the GROUP BY, does it produce the correct results (although unsorted to your liking)?Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog
Hmmm, no, I didn't try that. I took out Order from the GROUP BY and ORDER BY clauses, and it still didn't give me what I wanted.But once I took our the WHERE clause, it gave me what I wanted. So I guess it was the WHERE clause all along. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
Apples
Posting Yak Master
146 Posts |
Posted - 2008-07-31 : 16:35:24
|
quote: Originally posted by tkizer You aren't telling us the whole story then.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog
Please tell me what else you'd like to know.With the WHERE clause in there, it is showing the data, but not the data with 0 choicesWithout the WHERE clause, it is showing all the data, even the data with 0 choices. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-07-31 : 16:43:34
|
| Oh I see the issue now. You need to understand what an OUTER JOIN is doing. In your case, it is grabbing everything from Choices and then only the matches from Members. You are then trying to filter on Members. It is switching to an INNER JOIN due to the filter.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
Apples
Posting Yak Master
146 Posts |
Posted - 2008-07-31 : 16:51:16
|
quote: Originally posted by tkizer Oh I see the issue now. You need to understand what an OUTER JOIN is doing. In your case, it is grabbing everything from Choices and then only the matches from Members. You are then trying to filter on Members. It is switching to an INNER JOIN due to the filter.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog
Is there another way that I can filter the results? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-07-31 : 17:04:38
|
[code]SELECT c.ChoiceName, COUNT(m.ChoiceID)FROM tblMembers as mRIGHT JOIN tblChoices AS c ON c.ChoiceID = m.ChoiceID AND MONTH(m.JoinDate) = @Month AND YEAR(m.JoinDate) = @YearGROUP BY c.ChoiceName, c.OrderORDER BY c.Order[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|