【发布时间】:2020-03-13 07:57:31
【问题描述】:
我正在学习 SQL。现在我有一个包含列的表:user_id、event_timestamp 和 event_name。我需要统计每个月的新用户 (I) 和第二个月返回站点的用户 (II)(例如,如果用户第一次出现是在 2 月份,他们在 3 月份使用该站点,则应该统计他们。我想我计算了第一列(I),但我不知道如何计算第二列。 因此,结果应该有一个包含“月和年”、“每月新用户”和“returning_users”列的表。
select
distinct date_trunc('month', u.date_timestamp) as month_and_year,
count(*) as count_users
from (select distinct on (t.user_id) *
from example_table.table as t
order by t.user_id, t.date_timestamp
) as u
group by month_and_year
order by month_and_year
所以,答案中的解决方案有效,但我仍然有问题。我不确定,但我认为它并不像我想要的那样工作。我在这样的真实基础上尝试过:
select date_trunc('month', u.ship_date) as month_and_year,
count(distinct case when date_trunc('month', u.ship_date) = date_trunc('month', u.min_date) then cust_id end) as num_starts,
count(distinct case when date_trunc('month', u.ship_date) = date_trunc('month', u.min_date + interval '1 month') then cust_id end) as num_returning
from (select sh.*,
min(ship_date) over (partition by cust_id) as min_date
from shipping.shipment as sh
) u
group by month_and_year
order by month_and_year
我有一张这样的桌子:
+----------------------------+------------+---------------+
| month_and_year | num_starts | num_returning |
+----------------------------+------------+---------------+
| January 1, 2016, 12:00 AM | 6 | 0 |
+----------------------------+------------+---------------+
| February 1, 2016, 12:00 AM | 8 | 1 |
+----------------------------+------------+---------------+
| March 1, 2016, 12:00 AM | 16 | 0 |
+----------------------------+------------+---------------+
| April 1, 2016, 12:00 AM | 29 | 1 |
+----------------------------+------------+---------------+
| May 1, 2016, 12:00 AM | 23 | 9 |
+----------------------------+------------+---------------+
| June 1, 2016, 12:00 AM | 13 | 10 |
+----------------------------+------------+---------------+
| July 1, 2016, 12:00 AM | 4 | 5 |
+----------------------------+------------+---------------+
| August 1, 2016, 12:00 AM | 0 | 2 |
+----------------------------+------------+---------------+
如您所见,看起来 7 月和 8 月返回的用户比刚出现的用户多。那是因为这个查询显示谁在本月返回,但我想知道,例如,在 2 月出现的有多少人在 下 个月返回(所以, f.e. 三月)。我想现在第二个数字在 num_returning 的下面一行。你能帮我做对吗?
【问题讨论】:
-
样本数据和期望的结果会有所帮助。
标签: sql postgresql