我有两个桌子。
搜索:
user | eventtime
a | 25/02/19 00:00
a | 25/02/19 00:03
a | 25/02/19 00:05
b | 25/02/19 00:00
b | 25/02/19 00:04
b | 25/02/19 00:07
Detail_hits
user | eventtime
a | 25/02/19 00:04
b | 25/02/19 00:04
b | 25/02/19 00:05
现在,对于每个详细点击的条目,我都希望将其与搜索时间相对应,这是该用户在详细点击时间上或该用户之前的最后一次搜索。
所以输出将类似于
user | eventtime | searchtime
a | 25/02/19 00:04 | 25/02/19 00:03
b | 25/02/19 00:04 | 25/02/19 00:04
b | 25/02/19 00:05 | 25/02/19 00:04
到目前为止,我已经尝试过
select user, detail_hits.eventtime,
max(searches.eventtime) over(partition by user)
from detail_hits
left join searches using(user)
但这不起作用。
答案 0 :(得分:1)
一种方法使用left join
和聚合:
select dh.user, dh.eventtime, max(s.eventtime) as searchtime
from detail_hits dh left join
searches s
on dh.user = s.user and s.eventtime < dh.eventtime
group by dh.user, dh.eventtime;