sql - Postgres select (group by type) -


this question has answer here:

i have table like:

id     name      type  rating 1      name1      1      98 2      name2      1      17 3      name3      2      77 4      name4      2      53 5      name5      2      23 6      name6      4      64 7      name7      3      78 8      name8      3      56 9      name9      3      22 10     name10     4      56 11     name11     4      99 .      ...        .      .. 

how can select table, , example (2,3...etc, n) rows of each 'type' highest rating?

result example(for 2 rows):

id     name      type  rating 1      name1      1      98 2      name2      1      17 3      name3      2      77 4      name4      2      53 7      name7      3      78 8      name8      3      56 6      name6      4      64 11     name11     4      99 .      ...        .      .. 

you use row_number window function assign ranking each row, per type:

select id, name, type, rating   (select id, name, type, rating,                row_number() on (partition type order rating desc) rn           myable)  rn <= 3; -- or other number of rows per type wish 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -