update new_info as n set money = (select money from history_info hi where hi.id=n.id orderby hi.time desclimit1), age = (select age from history_info hi where hi.id=n.id orderby hi.time desclimit1), time = (selecttimefrom history_info hi where hi.id=n.id orderby hi.time desclimit1)
方式二:多表联动update。效率高
1 2 3 4 5
update new_info as n set money = m.money, age = m.age from (selectid,money,age,timefrom history_info hi where (id,time) in (selectid,max(time) from history_info groupbyid) ) as m where n.id=m.id
with MaxScorePerStudent as ( select id, name, score, subject, createtime, row_number() over ( partitionbyid orderby score desc, createtime desc ) as row_num from scores where score > 80
)
select id, name, score, subject, createtime from MaxScorePerStudent where row_num = 1
3.) 查询:分片表union成视图,进行数据分析
学生成绩表,每天会将昨天的历史成绩归档到 scores$yyyyMMdd 的表中。
但是,在一些场景中,还是需要将这些分片表合并成视图后,进行数据分析。此处最简单的做法就是 union all 实现。