I have a table of group_id, member_id, and created_at.
I'm trying to track the growth in group membership across time. Since group_id's are created when the first member_id joins, the min(created_at) for a given group should give the created date. I think this broken code gets the point across for what I'm trying to do (at the month level in this case):
SELECT brand_id,
min(created_at) as created_date,
min(created_at) + INTERVAL '1 month' as end_date,
count(member_id)
FROM member_group
HAVING created_at < end_date
group by 1
It seems to me that you are looking for a query like this:
SELECT g.brand_id, x.created_date, x.end_date, count(g.member_id)
FROM member_group g
JOIN (
SELECT brand_id,
min(created_at) as created_date,
min(created_at) + INTERVAL '1' month as end_date
FROM member_group
GROUP BY brand_id
) x
ON ( g.brand_id = x.brand_id
AND g.created_at BETWEEN x.created_date AND x.end_date )
GROUP BY g.brand_id, x.created_date, x.end_date
select
brand_id,
count(created_at < brand_date + interval '1 month' or null) as total
from
member_group
inner join (
select brand_id, min(created_at) as brand_date
from member_group
group by 1
) s using (brand_id)
group by 1
order by 1
;
Related
I have a table with:
ID (id client), date_start (subscription of SaaS), date_end (could be a date value or be NULL).
So I need a cumulative count of active clients month by month.
any idea on how to write that in Postgres and achieve this result?
Starting from this, but I don't know how to proceed
select
date_trunc('month', c.date_start)::date,
count(*)
from customer
Please check next solution:
select
subscrubed_date,
subscrubed_customers,
unsubscrubed_customers,
coalesce(subscrubed_customers, 0) - coalesce(unsubscrubed_customers, 0) cumulative
from (
select distinct
date_trunc('month', c.date_start)::date subscrubed_date,
sum(1) over (order by date_trunc('month', c.date_start)) subscrubed_customers
from customer c
order by subscrubed_date
) subscribed
left join (
select distinct
date_trunc('month', c.date_end)::date unsubscrubed_date,
sum(1) over (order by date_trunc('month', c.date_end)) unsubscrubed_customers
from customer c
where date_end is not null
order by unsubscrubed_date
) unsubscribed on subscribed.subscrubed_date = unsubscribed.unsubscrubed_date;
share SQL query
You have a table of customers. With a start date and sometimes an end date. As you want to group by date, but there are two dates in the table, you need to split these first.
Then, you may have months where only customers came and others where only customers left. So, you'll want a full outer join of the two sets.
For a cumulative sum (also called a running total), use SUM OVER.
with came as
(
select date_trunc('month', date_start) as month, count(*) as cnt
from customer
group by date_trunc('month', date_start)
)
, went as
(
select date_trunc('month', date_end) as month, count(*) as cnt
from customer
where date_end is not null
group by date_trunc('month', date_end)
)
select
month,
came.cnt as cust_new,
went.cnt as cust_gone,
sum(came.cnt - went.cnt) over (order by month) as cust_active
from came full outer join went using (month)
order by month;
I am working with members data. I have the responsible Coach, the coachee entry, exit status and date. Because some coachees might graduate/leave during a month I want to calculate a daily number and then get a monthly average of active members for each coach. That means that I need to take in the account all coachees from previous months, that are still active that current month. This is my data:
I am thinking of creating a variable first where I can get the daily active member count for each coach. This is my first approach:
with all_years as (
select y.year, m.month, d.day
from generate_series(2019, 2022) as y(year)
cross join generate_series(1, 12) as m(month)
cross join generate_series(1, 31) as d(day) --<<*not sure how to adjust for days with less than 31 days??*
select ay.*, coach, coachee, entry_status, entry_date, exit_reason, exit_date, sum(count) over (partition by ay.coach order by ay.year, ay.month, ay.day)
from all_years ay
left join table t
on --.... *not sure what I can join on in this case*;
I am open to an easier approach, this logic is just an idea.
You can cross join the list of distinct coaches with all dates to generat combinations, then bring the table with a left join:
select d.dt, c.coach, count(t.coach) no_coachees
from (select distinct coach from mytable) c
cross join generate_series('2019-01-01'::date, '2022-12-31'::date, '1 day':: interval) d(dt)
left join mytable t on t.coach = c.coach and t.entry_date <= d.dt and t.exit_date > d.dt
group by d.dt, c.coach
Then you can use another level of aggregation to get the monthly average:
select date_trunc('month', d.dt) d_month, coach, avg(no_coachees) avg_coaches
from (
select d.dt, c.coach, count(t.coach) no_coachees
from (select distinct coach from mytable) c
cross join generate_series('2019-01-01'::date, '2022-12-31'::date, '1 day':: interval) d(dt)
left join mytable t on t.coach = c.coach and t.entry_date <= d.dt and t.exit_date > d.dt
group by d.dt, c.coach
) t
group by date_trunc('month', d.dt), coach
I have a table for persons and other for visits. I need to count the visits for the month only if it was the first one. So basically get a count of visits for the month but only if your visit for the month, was the first.
Example:
PEOPLE:
id
---------
20
30
23
VISITS
id | date
-------------------------
20 | 09-20-2019
20 | 10-01-2019
23 | 10-09-2019
30 | 10-07-2019
I want to know the coutning only if its the first one, on this example, the counting should equal to 2 because person with ID 20 had a past visit last month.
This is what I have so far on my query
SELECT * FROM visits
LEFT JOIN people ON visits.id = people.id
WHERE date_trunc('month', current_date) <= visits.visit_date AND visits.visit_date < date_trunc('month', current_date) + INTERVAL '1 month'
This is just giving me the visits for the month. How can I filter by only if the person doesnt have past visits.
You can simplify the condition by just comparing months. Add not exists to check whether the person had a visit before the current month.
select *
from visits
left join people on visits.id = people.id
where date_trunc('month', current_date) = date_trunc('month', visits.visit_date)
and not exists (
select from visits
where id = people.id
and date_trunc('month', current_date) > date_trunc('month', visits.visit_date)
)
from what I understand on your requirement you just need to get the person Ids that had previous visit.
my proposal is to use dense_rank() to based on date_trunc('month', visit_date)
select v.id from visits v
inner join
(select dense_rank() over (partition by date_trunc('month', visit_date) order by visit_date) as rn
, *
from visits) t1 on t1.id = v.id
group by v.id, t1.rn
having count(v.id) > 1
Having a little trouble with a query provided by Periscope. Can you help point me in the right direction?
Error is - ERROR: aggregate functions are not allowed in GROUP BY Position: 305
with monthly_activity as (
select distinct
date_trunc('month', created_at) as month,
user_id
from oauth_refresh_tokens
),
first_activity as (
select user_id, date(min(created_at)) as month
from oauth_refresh_tokens
group by 2
)
select
this_month.month,
count(distinct user_id)
from monthly_activity this_month
left join monthly_activity last_month
on this_month.user_id = last_month.user_id
and this_month.month = last_month.month + interval '1 month'
join first_activity
on this_month.user_id = first_activity.user_id
and first_activity.month != this_month.month
where last_month.user_id is null
group by 1
I'm trying to find the # users who did action A or action B on a monthly basis.
Table: User
- id
- "creationDate"
Table: action_A
- user_id (= user.id)
- "creationDate"
Table: action_B
- user_id (= user.id)
- "creationDate"
The general idea of what I was trying to do was that I'd find the list of users who did action A in Month X and the list of users who did action B in Month X, then count how many ids are there for every month based on a generate_series of monthly dates.
I tried the following, however, the query times out when running and I'm not sure if there's any way to optimize it (or if it is even correct).
SELECT monthseries."Month", count(*)
FROM
(SELECT to_char(DAY::date, 'YYYY-MM') AS "Month"
FROM generate_series('2014-01-01'::date, CURRENT_DATE, '1 month') DAY) monthseries
LEFT JOIN
(SELECT to_char("creationDate", 'YYYY-MM') AS "Month",
id
FROM action_A) did_action_A ON monthseries."Month" = did_action_A."Month"
LEFT JOIN
(SELECT to_char("creationDate", 'YYYY-MM') AS "Month",
id
FROM action_B) did_action_B ON monthseries."Month" = did_action_B."Month"
GROUP BY monthseries."Month"
Any comments/ help would be immensely helpful!
If you want to count distinct users:
select to_char(month, 'YYYY-MM') as "Month", count(*)
from
generate_series(
'2014-01-01'::date, current_date, '1 month'
) monthseries (month)
left join (
(
select distinct date_trunc('month', "creationDate") as month, id
from action_a
) a
full outer join (
select distinct date_trunc('month', "creationDate") as month, id
from action_b
) b using (month, id)
) s using (month)
group by 1
order by 1