× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

SQL Count on Multiple Fields

Cristian Lupașcu
Mar 04, 2015
<p>Here's how you can do it in a single query:</p> <pre><code>select UniversityId, count(*) as `Total Users`, sum(case when gender = 'male' then 1 else 0 end) as Male, sum(case when gender = 'female' then 1 else 0 end) as Female from User group by UniversityId </code></pre> <p>Demo: <a href="http://www.sqlfiddle.com/#!2/e7d16a/4" rel="nofollow">http://www.sqlfiddle.com/#!2/e7d16a/4</a></p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/26056100/SQL%20Count%20on%20Multiple%20Fields/26056224">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus