场景:我有一个表events_table,该表包含由Webhook根据我发送给用户的消息插入的记录:
“ column_name”(type)-“ time_stamp”(带时区的时间戳)-“用户名”(varchar)-“已交付”(int)-“动作”(int)样本数据:
| time_stamp | username | delivered | action |
|:----------------|:---------|:----------|:-------|
|1349733421.460000| user1 | 1 | null |
|1549345346.460000| user3 | 1 | 1 |
|1524544421.460000| user1 | 1 | 1 |
|1345444421.570000| user7 | 1 | null |
|1756756761.980000| user9 | 1 | null |
|1234343421.460000| user171 | 1 | 1 |
|1843455621.460000| user5 | 1 | 1 |
| ... | ... | ... | ... |
“ delivered”列默认为空,当delivered时为1。默认情况下,“操作”列为空,当打开时,该列为1。
问题:
使用PostgreSQL,我如何计算从每周一开始的前30天内打开电子邮件的人数?理想查询结果:
这是我尝试过的程度,可以给我上周的计数:| date | count | |:----------------|:----------| | 02/24/2020 | 1,234,123 | | 02/17/2020 | 234,123 | | 02/10/2020 | 1,234,123 | | 02/03/2020 |12,341,213 | | ... | ... |
我的尝试:
SELECT
date_trunc('week', to_timestamp("time_stamp")) as date,
count("username") as count,
lag(count(1), 1) over (order by "date") as "count_previous_week"
FROM events_table
WHERE "delivered" = 1
and "action" = 1
GROUP BY 1 order by 1 desc
场景:我有一个表,events_table,其中包含一个由Webhook根据我发送给我的用户的消息插入的记录:“ column_name”(类型)-“ time_stamp”(带有时区的时间戳)-...
这是我编写此查询的尝试。