I have a mysql table , which holds data like this.
Name |how_out |runs
------------------------------
Robin |Not out |45
Robin |lbw |25
Robin |caught |50
Robin |caught |0
Robin |bowled |30
Robin |bowled |0
.
.
.
I want one SQL query, which returns the following columns
Name , Total_runs , Average
Sounds simple enough, but the only problem for me is that.
In cricket , average is just not Sum( runs)/count(*)
it’s Sum(runs)/Count of number of innings where how_out is not ‘Not out’
Could some one tell me how to write one such query ?
The query that i wrote gave the wrong average for obvious reasons.
The query that i wrote was
Select `Name`,sum(`runs`) as 'Total_runs', Sum(`runs`)/count(*)
From `batsman_stats`
group by `Name`
order by 2 desc
Could sql experts please help me ?
Thanks
1 Answer