Reputation: 1998
You may need some knowledge of cricket to help with this one. Given the following table, would this query produce this result?
Table: batsmen
batsmanname | runsscored | howout
Colin | 10 | bowled
Colin | 20 | caught
Steve | 10 | bowled
Steve | 20 | not out
SELECT batsmanname, SUM(runsscored) / COUNT(howout) AS battingavg
FROM batsmen
WHERE howout <> 'not out'
GROUP BY batsmananme
Result:
batsmanname | battingavg
Colin | 15
Steve | 30
Upvotes: 0
Views: 383
Reputation: 247810
This should produce the results that you want:
SELECT b1.name, SUM(b1.runsscored) / b2.NumOut as bat_avg
FROM batsmen b1
INNER JOIN
(
select name, COUNT(howout) as NumOut
from batsmen
WHERE howout <> 'not out'
GROUP BY name
) b2
ON b1.name = b2.name
GROUP BY b1.name
Upvotes: 1