如何将结果数限制为每个 'user' 仅 5 个,但如果第 5 行的立即值与连续行相同,则允许更多行?

How to limit the no of results to just 5 per 'user' but allow for more rows if the immediate value at 5th row is same as the consecutive rows?

我有一个数据库Domain_id,一个组织的工资和工资日期。 我的问题是我必须找到每个域的 5 个最高薪水,但是如果直接的第 6、7 或更多行与第 5 行具有相同的值,则行数(5 行)可以增加到更多。我尝试使用 window 函数。

select * 来自 (SELECT id_domain, salary_value,salary_date, dense_rank() 超过( 分区 id_domain 按 salary_date 降序排序,salary_value 降序 ) AS comment_rank 来自员工) comment_rank <=5

的员工

我试过在这种情况下使用滞后和超前,仍然无法弄清楚

select * from (SELECT id_domain,salary_value, salary_value - lag(salary_value) 
OVER (
     PARTITION BY id_domain
     ORDER BY salary_value desc
) AS diff, row_number() over (partition by id_user order by salary_value desc) as rowrank
FROM employee)as t 
case when rowrank > 5
where (case when diff <> 0 then rowrank > 5 else false end)

使用rank()。如果你想要 5+ 那么:

select *
from (select id_domain,salary_value,
             salary_value - lag(salary_value) over (partition by id_domain order by salary_value desc) AS diff,
             rank() over (partition by id_user order by salary_value desc) as rowrank
      from employee)as t 
     ) t
where rowrank < 6;

或:

where rowrank <= 5