Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
select CANDIDATE.Candidate_Party, COUNT(CANDIDATE.Candidate_Number) AS VotesNumber
from CANDIDATE
left outer join VOTE on CANDIDATE.Candidate_Number = VOTE.Candidate_Number
WHERE
CANDIDATE.Candidate_Portfolio = 'Missionvale Representative'

 GROUP BY CANDIDATE.Candidate_Party



My sql above, everytime when it counting the there is no data in a database is display 1, what i want is to return a 0 if there is no data.

i tryed lot of querys but they ddnt work. Please help.

When m counting it must display 0
Posted
Comments
[no name] 29-Aug-13 5:51am    
http://www.codeproject.com/Questions/643296/How-to-count-and-display-0
nikhil-vartak 29-Aug-13 5:52am    
Your requirement is not clear. Can you re-phrase it to make it more understandable?
ZurdoDev 29-Aug-13 7:48am    
It shows 1 because there is 1 record. It is likely coming from your left join. Remove the COUNT() and the group by and see what comes back. You should get 1 record.
Bernhard Hiller 29-Aug-13 9:59am    
Do not know the exact syntax now. You need two subqueries: subquery 1 is
SELECT DISTINCT Candidate_Party FROM Candidate, subquery 2 is essentially your query above (replace LEFT JOIN with OUTER JOIN).
Then you need to join the results of the two subqueries, with a CASE statement selecting 0 as the count when subquery 2 does not contain a corresponding record.
Hope someone else is so fluent with SQL to write that down.

1 solution

I think it's because of the type of join you are using. A left outer join is ALWAYS going to give you EVERY candidate (because it's the table on the left) and then will also give you any votes they made. What you want is an inner join, which will only give you data that matches both files.

http://stackoverflow.com/questions/38549/difference-between-inner-and-outer-join[^]

http://www.google.com/#q=sql+outer+join+vs+inner+join[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900