How to calculate percentages in tsql using a group by query?
<p>I have a table that lists results by sport and start and end dates and win/loss result. I need to calculate the percentage of wins per sport per season_start. This is my table:</p>

<pre><code>table1
******
id    sport  won   playerid     season_start  season_end    updated_date
---   -----  ----  --------     ------------ ------------  ------------
1     rugby   Y    kato23       2016-01-01     2016-01-31   2016-02-01
2     rugby   Y    king54       2016-01-01     2016-01-31   2016-02-01
3     rugby   N    robby1       2016-03-01     2016-03-28   2016-04-01
4     rugby   Y    kelly2       2016-03-01     2016-03-28   2016-04-01
5     soccer  Y    kato23       2016-01-01     2016-01-31   2016-02-01
6     soccer  Y    jeri44       2016-01-01     2016-01-31   2016-02-01
7     soccer  N    matt24       2016-06-01     2016-06-30   2016-07-01
8     tennis  Y    kray43       2016-01-01     2016-01-31   2016-02-01
9     tennis  Y    jeri44       2016-01-01     2016-01-31   2016-02-01
10    tennis  N    jeri44       2016-01-01     2016-01-31   2016-02-01
</code></pre>

<p>So I'd like to run a query that produces the following result:</p>

<pre><code>sport     season_start     success_percent
-----     ------------     ---------------
rugby     2016-01-01        100.00
rugby     2016-03-01        50.00
soccer    2016-01-01        100.00
soccer    2016-06-01        0.00
tennis    2016-01-01        66.66
</code></pre>

<p>I tried :</p>

<pre><code>select sport,won, season_start, count(sport) as sportcount
from table1 where won = 'Y'
group by sport, won, season_start
</code></pre>

<p>But this just give me the entire count but not the actual success percent. I tried using case statement but I'm not too good with tsql, or with using 'Sum' to tally the wins and the losses and then calculate the win percentage out of the total. </p>

<p>Can someone provide a tsql query that does this?</p>