mar*_*2x4 1 sql postgresql data-partitioning snowflake-cloud-data-platform
我有下面的数据集,其中包含必须返回哪一行的注释。
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
Run Code Online (Sandbox Code Playgroud)
逻辑是:返回每个国家分区内最大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
Run Code Online (Sandbox Code Playgroud)
问题:
对于 Snowflake,您还可以避免窗口函数上的子查询,而只需使用 QUALIFY 函数:
select r.*
from rates r
QUALIFY row_number() over (partition by country order by kg_to desc) = 1;
Run Code Online (Sandbox Code Playgroud)