获取给定日期之前的 运行 项的唯一计数,类似于 运行 总数,但取而代之的是 运行 唯一计数

Get the running unique count of items till a give date, similar to running total but instead a running unique count

我有一个 table 用户购物数据如下所示

我想要一个类似于 运行 总计的输出,但我想要 运行 用户按日期购买的唯一类别的总数。

我知道我必须在 count 函数中使用 ROWS PRECEDING AND FOLLOWING 但我无法在 window 函数中使用 count(distinct category)

Dt  category    userId
4/10/2022   Grocery 123
4/11/2022   Grocery 123
4/12/2022   MISC    123
4/13/2022   SERVICES    123
4/14/2022   RETAIl  123
4/15/2022   TRANSP  123
4/20/2022   GROCERY 123

期望的输出

Dt  userID  number of unique categories
4/10/2022   123 1
4/11/2022   123 1
4/12/2022   123 2
4/13/2022   123 3
4/14/2022   123 4
4/15/2022   123 5
4/20/2022   123 5

考虑以下方法

select Dt, userId, 
  ( select count(distinct category)
    from t.categories as category
  ) number_of_unique_categories
from (
select *, array_agg(lower(category)) over(partition by userId order by Dt) categories
from your_table
) t               

如果应用于您问题中的示例数据 - 输出为