Postgresql: How to select the top n percent (%) of records from each group / category

We are new to postgres, we have the following query by which we can select the top N entries from each category.

 create table temp (
     gp char,
     val int
 );

 insert into temp values ('A',10);
 insert into temp values ('A',8);
 insert into temp values ('A',6);
 insert into temp values ('A',4);
 insert into temp values ('B',3);
 insert into temp values ('B',2);
 insert into temp values ('B',1);

 select a.gp,a.val
 from   temp a
 where  a.val in (
              select b.val
              from   temp b
              where  a.gp=b.gp
              order by b.val desc
             limit 2);

The query output above is something like this

 gp   val
 ----------
 A    10
 A    8
 B    3
 B    2

But our requirement is different, we want to select the top n% of records from each category, where n is not fixed, n is based on a certain percentage of elements in each group.

+5
source share
3 answers

To get rows based on the percentage of the number of rows in each group, you can use two window functions: one to count the rows and one to give them a unique number.

select gp,
       val
from (
  select gp, 
         val,
         count(*) over (partition by gp) as cnt,
         row_number() over (partition by gp order by val desc) as rn
  from temp
) t
where rn / cnt <= 0.75;

SQLFiddle: http://sqlfiddle.com/#!15/94fdd/1


Btw: char - , , . , .

+16

a_horse_with_no_name, - , _()

SELECT
    gp,
    val,
    pct_rank
FROM (
    SELECT
        gp,
        val,
        percent_rank() over (order by val desc) as pct_rank
    FROM variables.temp
    ) t
WHERE pct_rank <= 0.75;

WHERE, _().

0

. , :

SELECT * FROM temp ORDER BY val DESC
     LIMIT (SELECT (count(*) / 10) AS selnum FROM temp )

(),

-1

Source: https://habr.com/ru/post/1547545/


All Articles