在聚合整型列时,对文本列中最后一项的查询进行分组



有人能帮我汇总int列,并采取col4文本值对应于最新的col2日期列。

假设我们有如下表'Table1':

<表类> col1 col2 col3 col4 tbody><<tr>名称2021-01-0150First_Appearance名称2021-01-0260Second_Appearance名称2021-01-0440Third_Appearance2021-01-0150First_Appearance2021-01-0360Second_Appearance2021-01-0540Third_Appearance2021-01-0440Fourth_Appearance

一种方法是使用row_number():

进行条件聚合
select col1, max(col2), sum(col3),
max(case when seqnum = 1 then col4 end) as col4
from (select t.*,
row_number() over (partition by col1 order by col2 desc) as seqnum
from t
) t
group by col1;

相关内容

  • 没有找到相关文章

最新更新