简体   繁体   English

如何 select 只有一个名称与 Redshift SQL 中的 ID 配对

[英]How to select only one name paired to the ID in Redshift SQL

I have a dataset in which each id is paired with more than 1 name and in some cases the name is null. I need to decouple it to select just once the ID and the most recent updated name.我有一个数据集,其中每个 ID 都与超过 1 个名称配对,在某些情况下,名称是 null。我需要将它与 select 解耦一次 ID 和最近更新的名称。

id ID name名称 last_updated最近更新时间
001 001 name1姓名1 04/01/2022 04/01/2022
001 001 name2名字2 05/01/2022 05/01/2022
001 001 NULL NULL 06/01/2022 06/01/2022
002 002 name3名字3 04/01/2022 04/01/2022
002 002 name4姓名4 05/01/2022 05/01/2022
002 002 NULL NULL 06/01/2022 06/01/2022

In this case, I need to select the distinct IDs (001, 002) and the most recent name which is not null在这种情况下,我需要 select 不同的 ID(001、002)和不是 null 的最新名称

id ID name名称 last_updated最近更新时间
001 001 name2名字2 05/01/2022 05/01/2022
002 002 name4姓名4 05/01/2022 05/01/2022

It is a typical task using SQL window functions.这是使用 SQL window 函数的典型任务。

We rank every row based on id and order them by date column.我们根据 id 对每一行进行排名,并按日期列对它们进行排序。 After it, we choose only rows with the first rank之后,我们只选择排名第一的行

with temp as (select *,
                     rank() over (partition by id order by last_updated desc) as rank
              from test.temp
              where name is not null)
select id, name, last_updated
from temp
where rank = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM