Sql 从group by中获取一条记录,而无需手动键入所有列
我有一个包含以下列的表:Sql 从group by中获取一条记录,而无需手动键入所有列,sql,hiveql,presto,Sql,Hiveql,Presto,我有一个包含以下列的表: id, col_1, col_2, col_3, .... col_1000, timestamp 我希望每个id只有一条记录,使用最新的时间戳。也就是说,我的问题是: select id, max_by(col_1, timestamp), max_by(col_2, timestamp), max_by(col_3, timestamp), ... max_by(col_1000, timestamp), max_by(timestamp, timestamp)
id, col_1, col_2, col_3, .... col_1000, timestamp
我希望每个id只有一条记录,使用最新的时间戳。也就是说,我的问题是:
select id, max_by(col_1, timestamp), max_by(col_2, timestamp), max_by(col_3, timestamp), ... max_by(col_1000, timestamp), max_by(timestamp, timestamp) group by id
既然有这么多专栏,我想知道有没有一种方法可以做到:
select max_by(*, timestamp) group by id
这样我就不必手动键入所有列了?谢谢 嗯。您可以使用:
您还可以使用相关子查询或join
来表示这一点:
select t.*
from t join
(select id, max(timestamp) as max_timestamp
from t
group by id
) tt
on t.id = tt.id and t.timestamp = tt.max_timestamp
select t.*
from t join
(select id, max(timestamp) as max_timestamp
from t
group by id
) tt
on t.id = tt.id and t.timestamp = tt.max_timestamp