sql - Only want the first if several matches are returned -
i have sql script returns number of records. code:
select distinct eb.exerciseindex, eb.exercisestarttime, avg(pb.playerbirthyear) birthavg, count(pb.playerindex) playersinexercise, pb.playersex playerbase pb inner join exbase ex on ex.explayerindex = pb.playerindex inner join exercisebase eb on eb.exerciseindex = ex.exexerciseindex exerciseallowanceindex='b26e10c5-53e1-413a-8a49-a7088d33e690' , exercise_is_paying='true' group exercisestarttime, pb.playersex, eb.exerciseindex order exerciseindex, playersinexercise desc
i have attached image results.
now, want kind of distinct on results. if note, on occasions several rows returned same exerciseindex (see row 1 , 2 example). need first row exerciseindex returned (the 1 higher playercount).
any ideas how can done?
regards,
bob
you should neat little so-called window function called row_number.
using this, like:
select * ( select *, row_number() on (partition exerciseindex order playersinexercise desc) n ( -- original query goes here - except order clause -- ) t1 ) t2 t2.n = 1 -- put order clause here --
Comments
Post a Comment