题目要求
id cnt url
1 12 a
2 21 f
1 32 e相关说明:
cnt相当于日期
这里需要把这个表拆分成如下的类型
id 最大日期 url 最小日期 url
1 32 e 12 a
当时脑子突然一热,在面试官面前有点紧张,这道题也就没有做出来,面试完之后自己又重新审视了一下这道题,具体的sql语句如下
select
t3.id,t3.cnt,t3.url,t4.cnt,t4.url
from
(select
t1.*
from
(select
id,cnt,url,row_number() over(partition by id order by cnt) temp
from info) t1
where t1.temp <2 ) t3
join
(select
t2.*
from
(select
id,cnt,url,row_number() over(partition by id order by cnt desc) temp
from info) t2
where t2.temp <2) t4
on t3.id = t4.id
这道题主要的是要把多余的列剔除掉,表中只保留最大列和最小列。