如何选择动态排名值?

问题描述 投票:0回答:1

我有一张公司和另一名员工的表以及他们加入公司的日期。我能够为每家公司as shown here获得最近5名员工。现在,我想只显示公司A的等级<= 3,公司B的等级<= 8和公司C的无限数量.3,8和-1被存储为公司表中的“最大”列。在这种情况下如何动态选择最大值?

sql postgresql postgresql-9.6
1个回答
0
投票

你基本上想要:

SELECT *  -- choose the columns you want here
FROM (SELECT e.*, c.max,
             row_number() over (partition by company order by joined desc) as rank
      FROM employees e JOIN
           companies c
           on e.company = c.pk
     ) e
WHERE rank < max or max = -1
© www.soinside.com 2019 - 2024. All rights reserved.