有人能帮我汇总int列,并采取col4文本值对应于最新的col2日期列。
假设我们有如下表'Table1':
<表类>
col1
col2
col3
col4
tbody><<tr>名称 2021-01-01 50 First_Appearance 名称 2021-01-02 60 Second_Appearance 名称 2021-01-04 40 Third_Appearance 2021-01-01 50 First_Appearance 2021-01-03 60 Second_Appearance 2021-01-05 40 Third_Appearance 2021-01-04 40 Fourth_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;