SQL-根据最大值返回分区中的行

问题描述

我下面有注释的数据集,必须返回哪一行。

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

逻辑是:每个国家/地区分区内的最大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. 更短的代码会更好,关于如何改进它的任何想法?

解决方法

对于Snowflake,您还可以避免使用window函数的子查询,而只需使用QUALIFY函数:

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

使用distinct on

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

或窗口功能:

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;

注意:我也看不到您的代码如何检索重复项,除非您的表中某个国家/地区有重复的最大值。

,

您需要在(t.country)上进行区分,以便每个国家/地区拥有一个记录。 order by确定每个国家选择哪一条记录。

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