cswork / libraryOpen practice workspace

Coding questions · Mixpanel

Aggregate User Events and Check Join Uniqueness

Aggregate user events and retain all users in PostgreSQL, computing event count, distinct active dates, earliest event, and join uniqueness.

Build a User Activity Summary from Event Data

You have two tables. The intended user summary contains one row per user_id, and the users table provides one record for each user.

TableColumnMeaning
usersuser_idUser identifier.
userssignup_dateThe user's signup date or timestamp.
usersplan_typeThe user's plan type.
eventsuser_idIdentifier of the user associated with an event.
eventsevent_tsTimestamp of the event.

Write one PostgreSQL query that returns every user together with that user's event summary. Group all available event rows by user_id and compute:

  • event_cnt: the number of non-NULL event_ts values.
  • active_days: the number of distinct calendar dates represented by non-NULL event_ts values. Use the date component of the stored timestamp.
  • first_event: the earliest non-NULL event_ts value.

Join the event summary to users so that users without any event rows are retained. For such users, the three event-summary fields are NULL; do not replace them with zero. Do not add a first-week restriction or any other time filter to the aggregation.

Return these columns in this order:

user_id, signup_date, plan_type, event_cnt, active_days, first_event.

The output row order is unrestricted. The query must not change either input table.

Clarifications

The event summary has one row per user_id. The final result must retain the intended one-row-per-user invariant; the practical exercise also asks you to check that merging the summaries has not introduced duplicate user identifiers.

Build a User Activity Summary from Event Data

Table users

columntypeconstraints
user_idINTEGERUNIQUE
signup_dateTIMESTAMP WITHOUT TIME ZONE
plan_typeTEXT

Sample data:

plan_typesignup_dateuser_id
free2024-02-28 12:00:00101
paid2024-03-01 00:00:00102
free2024-03-04 09:00:00103
trial2024-03-01 08:30:00104
paid2024-12-31 18:00:00105
NULL2025-01-02 12:00:00106
enterprise2024-03-15 10:00:00107

Table events

columntypeconstraints
user_idINTEGER
event_tsTIMESTAMP WITHOUT TIME ZONE

Sample data:

event_tsuser_id
2024-03-10 10:15:00101
2024-03-01 23:59:59102
2024-02-29 23:59:59101
NULL104
2025-01-01 00:00:00105
2024-02-29 23:59:59101
2023-08-01 07:15:00107
2024-03-01 00:00:00102
2024-02-28 11:59:59101
2024-03-03 14:00:00999
2024-03-01 00:00:00101
NULL104
2024-12-31 23:59:59105
2024-03-01 08:01:02102
NULL101
2024-06-01 12:00:00NULL
NULL107

Expected output

user_idsignup_dateplan_typeevent_cntactive_daysfirst_event
1012024-02-28 12:00:00free542024-02-28 11:59:59
1022024-03-01 00:00:00paid312024-03-01 00:00:00
1032024-03-04 09:00:00freeNULLNULLNULL
1042024-03-01 08:30:00trial00NULL
1052024-12-31 18:00:00paid222024-12-31 23:59:59
1062025-01-02 12:00:00NULLNULLNULLNULL
1072024-03-15 10:00:00enterprise112023-08-01 07:15:00