I have the following table:
horse_main_id | race_id | horse_name | rating100 | race_result ---------------+---------+-------------------+-----------+------------- 23 | 159 | Angelology | 126 | 1 24 | 159 | Arwoc | 118 | 25 | 159 | Marlo Man | 118 | 26 | 159 | Hucknall | 113 | 27 | 159 | It's Electrifying | 110 | 2 28 | 159 | Monte Carlo | 107 | 29 | 159 | Cross Constance | 103 | 30 | 160 | Auld Burns | 119 | 31 | 160 | Diamond Jim | 117 | 33 | 160 | Livery | 115 | 34 | 160 | Paraggi | 115 | 35 | 160 | I'll'ava'alf | 110 | 2 36 | 160 | Jacks 'n' Kings | 108 | 32 | 160 | Fastnet Isle | 115 | 1
I would like to operate on groups of race_id
, find all groups that have the max(rating100)
for their group and race_result = 1
, and count all the times this happens.
So this example should return only 1, as for race_id 159 group the max rating100 of 126 also has race_result = 1, but for race_id 160 group, the max rating100 is 119, but does not have race_result = 1.
This is the closest I've got (was told I should use window functions):
SELECT * FROM ( SELECT horse_name, race_id, race_result , max(rating100) OVER (partition by race_id) AS max_rating FROM horse_main) t WHERE race_result = 1;