I have two tables, clicks:
user date
a 01/10/2021
a 02/10/2021
a 03/10/2021
b 27/09/2021
c 08/10/2021
c 13/10/2021
and segment:
user date segment
a 26/09/2021 1
a 27/09/2021 1
a 03/10/2021 2
c 01/10/2021 5
c 10/10/2021 6
I want to join segment to clicks to identify the latest segment per user on that given click date. So the correct output will look like this:
user date segment
a 01/10/2021 1
a 02/10/2021 1
a 03/10/2021 2
b 27/09/2021
c 08/10/2021 5
c 13/10/2021 6
I have tried the following but get the error 'this type of correlated subquery pattern is not supported due to internal error'
select *,
(select top 1 segment
from b
where
b.date <= a.date
ORDER BY ROW_NUMBER() OVER(PARTITION BY b.id ORDER BY b.date DESC)) as segment_lookup
from a;
Any help greatly appreciated to find the best method. Thanks in advance