我需要在一段时间内计算sql(Vertica)中的不同值。 我有这样的表:命名为U
+---------+--------------+---------+
| user_id | dt | type_id |
+---------+--------------+---------+
| 1 | '2016-01-01' | 7 |
| 2 | '2016-01-01' | 7 |
| 3 | '2016-01-01' | 8 |
| 4 | '2016-01-03' | 7 |
| 3 | '2016-01-03' | 7 |
| 2 | '2016-01-03' | 8 |
| 1 | '2016-01-09' | 7 |
| 3 | '2016-01-09' | 7 |
| 5 | '2016-01-09' | 8 |
+---------+--------------+---------+
我认为和代码应该是
select (count(distinct(u.user_id)) over last 7 date(u.dt) ) as n_last_7_day, date(u.dt), u.type_id
from u
group by date(u.dt), u.type_id
我正在等待的输出就像:
+--------------+--------------+---------+
| n_last_7_day | dt | type_id |
+--------------+--------------+---------+
| 4 | '2016-01-01' | 7 |
| 2 | '2016-01-01' | 8 |
| 3 | '2016-01-03' | 7 |
| 2 | '2016-01-03' | 8 |
| 2 | '2016-01-09' | 7 |
| 1 | '2016-01-09' | 8 |
+--------------+--------------+---------+
因为它是'2016-01-01'中的用户1,2和'2016-01-03'中的3和4不到7天所以在outpu中我们有'2016-01-01的4号'和type_id 7