【发布时间】:2020-10-28 15:59:25
【问题描述】:
我有以下带有 cmets 的数据集,必须返回哪一行。
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
逻辑是:返回每个国家分区内最大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
问题:
- 更短的代码会更好,关于如何改进它的任何想法?
【问题讨论】:
-
Postgres 和 Snowflake 是不同的 SQL 产品。您真正使用的是哪个?
标签: sql postgresql snowflake-cloud-data-platform data-partitioning