I have the following structure:
table user
user_id | month_year | fruits
------------------------------
1 | 2021-01 | apple
1 | 2021-01 | melon
1 | 2021-01 | orange
1 | 2021-02 | grape
1 | 2021-02 | orange
1 | 2021-02 | kiwi
1 | 2021-03 | grape
1 | 2021-03 | pear
1 | 2021-03 | banana
1 | 2021-04 | orange
1 | 2021-04 | kiwi
1 | 2021-04 | banana
1 | 2021-05 | grape
1 | 2021-05 | pear
1 | 2021-05 | kiwi
And I want the following result:
user | month_year | fruits | two_months_most_freq
-------------------------------------------------------------------------
1 | 2021-01 | apple, melon, orange | orange
1 | 2021-02 | grape, orange, kiwi | orange
1 | 2021-03 | grape, pear, banana | grape
1 | 2021-04 | orange, kiwi, banana | banana
1 | 2021-05 | grape, pear, kiwi | kiwi
Clearing: In the last column I want the most recurrent fruit in the last 2 months period, in other words, the most repeated in the actual and previous line. Notice that in the first line should return orange because the window frame ahead should be used when the window frame behind is not available.
In the code below, I achieved the most recurrent fruit in the whole dataset.
select * from (
select user_id, year_month,
string_agg(distinct fruit) as fruits
from user
group by user_id, year_month
) join (
select user_id, fruit
from user
group by user_id, fruit
qualify 1 = row_number() over(partition by user_id order by count(*) desc)
)
using (user_id)
How can I apply this logic for specific time windows?
