【发布时间】:2021-07-02 10:17:36
【问题描述】:
我有一张这样的桌子:
| date | name |
|---|---|
| 1/1 | a |
| 1/2 | b |
| 1/3 | c |
| 1/2 | a |
| 1/3 | d |
并且需要从每行的日期开始计算 7 天内可用的不同名称。 结果应该是:
| date | count | explain |
|---|---|---|
| 1/1 | 1 | a |
| 1/2 | 2 | a,b |
| 1/3 | 4 | a,b,c,d |
我试过这些 count+if、count+case when、lag 函数:
- COUNT(DISTINCT(IF(date_1 between date - INTERVAL '7' DAY and date, name)))
- lag(count(distinct name), 7) over (order by date)
没有任何作用,结果只是每天计算的名字。
请指教,非常感谢!
【问题讨论】:
-
请标记您正在使用的数据库。
-
添加了 presto,非常感谢