需要一个SQL查询,该查询将与另一个表保持连接,该表将返回基于时间的最新值,并分组到一行中
我需要一个SELECT SQL查询的帮助,该查询将与id列上的另一个表保持连接,该表将根据时间返回最新的值,并分组为一行 基本上,连接两个表的方式是,对于时间序列表中存在的users表中的每条记录,根据分组到一行中的时间返回最新的值 用户表:需要一个SQL查询,该查询将与另一个表保持连接,该表将返回基于时间的最新值,并分组到一行中,sql,inner-join,greatest-n-per-group,presto,amazon-athena,Sql,Inner Join,Greatest N Per Group,Presto,Amazon Athena,我需要一个SELECT SQL查询的帮助,该查询将与id列上的另一个表保持连接,该表将根据时间返回最新的值,并分组为一行 基本上,连接两个表的方式是,对于时间序列表中存在的users表中的每条记录,根据分组到一行中的时间返回最新的值 用户表: id | name ----+-------- 1 | "Joe" 2 | "Ron" 时间序列表: time| id | a | b | c | d ----+-----+-----+
id | name
----+--------
1 | "Joe"
2 | "Ron"
时间序列表:
time| id | a | b | c | d
----+-----+-----+-----+----+----
1 | 1 | a1 | | |
2 | 1 | | b1 | |
3 | 1 | | | c0 |
4 | 1 | a3 | | c3 |
5 | 1 | a0 | | |
6 | 2 | a3 | | c3 | d1
7 | 2 | a2 | | | d3
结果应该如下所示:
id | a | b | c | d | name
----+-----+-----+----+----+------
1 | a0 | b1 | c3 | | "Joe"
2 | a2 | | | d3 | "Ron"
一个选项使用两个子查询:
select u.*,
(select ts.a from time_series ts where ts.id = u.id and ts.a is not null order by ts.time desc limit 1) a,
(select ts.b from time_series ts where ts.id = u.id and ts.b is not null order by ts.time desc limit 1) b,
(select ts.c from time_series ts where ts.id = u.id and ts.c is not null order by ts.time desc limit 1) c,
(select ts.d from time_series ts where ts.id = u.id and ts.d is not null order by ts.time desc limit 1) d
from users u
另一种解决方案是使用row\u number()
对每列的行进行排序,优先考虑非null
值,然后进行条件聚合:
select u.id,
max(case when ts.rn_a = 1 then ts.a end) a,
max(case when ts.rn_b = 1 then ts.b end) a,
max(case when ts.rn_c = 1 then ts.c end) a,
max(case when ts.rn_d = 1 then ts.d end) d
from users u
inner join (
select ts.*,
row_number() over(order by (case when a is null then 1 else 0 end), time desc) rn_a,
row_number() over(order by (case when b is null then 1 else 0 end), time desc) rn_b,
row_number() over(order by (case when c is null then 1 else 0 end), time desc) rn_c,
row_number() over(order by (case when d is null then 1 else 0 end), time desc) rn_d
from time_series ts
) ts on ts.id = u.id
group by u.id
我认为您正在寻找的函数是
coalesce
SELECT u.id
, coalesce(a)
, coalesce(b)
, coalesce(c)
, coalesce(d)
, name
FROM users u
LEFT JOIN time_series ts
ON u.id = ts.id
GROUP BY 1, 6
ORDER BY ts.time DESC;