SQL - return 分区中的行基于最大值

SQL - return rows in partition based on max value

我有下面的数据集,其中的行必须 returned。

INSERT INTO rates
  (country,kg_from,kg_to,value)
VALUES
  --('pl', '0', '5', '2.5'),
  --('pl', '5', '10', '4.5'),
  --('pl', '10', '15', '6'),
  --('pl', '15', '20', '8'), -- return this row
  --('de', '0', '5', '1.5'),
  --('de', '5', '10', '1.5'),
  --('de', '10', '15', '1.5'),
  --('de', '15', '45', '1.5'),  -- return this row
  --('cz', '0', '5', '5'),
  --('cz', '5', '10', '5'),
  --('cz', '10', '15', '6'),
  --('cz', '15', '30', '4') -- return this row

逻辑是:return 每个国家分区内的最大值 kg_to。

当前工作代码:

select t.country, t.kg_to, t.value
from rates t
inner join (select country, max(t2.kg_to) as max_kg
                      from rates t2
                      group by 1) t2 on t.country = t2.country
WHERE t.kg_to = t2.max_kg;
enter code here

问题:

  1. 代码越短越好,有什么改进的想法吗?

使用distinct on:

select distinct on (t.country) r.*
from rates r
order by t.country, kg_to desc;

或window函数:

select r.*
from (select r.*,
             row_number() over (partition by country order by kg_to desc) as seqnum
      from rates r
     ) r
where seqnum = 1;

注意:我也看不出您的代码如何检索重复项,除非您在 table.

中有一个国家/地区的重复最大值

您需要不同的 (t.country) 才能为每个国家/地区创建一条记录。 order by 确定每个国家选择哪一条记录。

select distinct on (country)
    country, kg_to, value
 from rates
 order by country, kg_to desc;

对于Snowflake,你也可以避免在window函数上使用sub-query,直接使用QUALIFY函数:

select r.*
from rates r
QUALIFY row_number() over (partition by country order by kg_to desc) = 1;