我有以下数据想要过滤,所以我只根据第一列的分组得到一行并选择最大日期
co2 包含独特的值
col1 | col2 | date
1 | 123 | 2013
1 | 124 | 2012
1 | 125 | 2014
2 | 213 | 2011
2 | 214 | 2015
2 | 215 | 2018
所以我想要的结果是:
1 | 125 | 2014
2 | 215 | 2018
我尝试使用在这里找到的一些示例(如下所示)以及其他 group by/distinct/max(date) 但没有运气
select t.*
from (select t.*,
row_number() over (partition by col1, col2 order by date desc) as seqnum
from t
) t
where seqnum = 1
更改分区row_number()
仅分区col1
但保持顺序date desc
:
select col1, col2, date
from
(
select col1, col2, date,
row_number() over (partition by col1
order by date desc) as rn
from yourtable
) x
where rn = 1
See SQL 摆弄演示 http://www.sqlfiddle.com/#!4/87390/2.
由于您同时进行了分区col1
and col2
您获得了每一行的唯一值。因此它不会返回具有最大日期的行。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)