2

I have this query...

SELECT SUM(brownlow_votes) AS votes, 
player_id, 
player_name, 
player_team, 
COUNT(*) AS vote_count 
FROM afl_brownlow_phantom, afl_playerstats 
WHERE player_id=brownlow_player 
AND brownlow_match=player_match 
GROUP BY player_id 
ORDER BY votes DESC LIMIT 50

So "votes" becomes the number of votes a player has, "vote_count" becomes the number of times (matches in which) a player has been voted for. This works fine.

However, I have another column called "brownlow_lock" which is either blank, or 'Y'. How do I get the number of occurances of 'Y'? I know I could solve this changing it to 0 or 1 and just doing a SUM() but I don't want to have to go and edit the tons of pages that are inserting data.

2 Answers 2

1

If I have understood you correctly you just need to add

COUNT(CASE WHEN brownlow_lock='Y' THEN 1 END) AS Cnt

to your query

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Martin, that only counts if there is a 'Y' for that player though? Sometimes they have more than 1...
Actually, I might be wrong here, I think I have a mistake in my query, it's not actually counting all the votes. I'll come back to this!
@Mark - If there is more than one Y within a group they will be counted. Is that the desired result?
The "1" in there is for counting, not the result. It will give you what you are after
0

Try using the IF control flow function

SELECT SUM(IF(brownlow_lock='Y',1,0)) lock_count ...

1 Comment

Hi Fredrik, I tried that but it didnt work right. The answer above works well though. Thanks anyway :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.