Coding time Calculate Daily Average Users (DAU). Consider the useractivity table with…
- User
Coding time Calculate Daily Average Users (DAU).
Consider the user_activity table with the following structure:
Table name: user_activity Columns: user_id: An integer representing the user's unique identifier. activity_dt: A timestamp representing the date and time of the user's activity. Write an SQL query to calculate the Daily Average Users (DAU) for each day within the date range from '2023-01-01' to '2023-01-07', inclusive. DAU is defined as the average number of unique users who performed activities per day.
Your query should return two columns:
date: The date in the format 'YYYY-MM-DD'. dau: The Daily Average Users for that date. Write an SQL statement
- Assistant
To calculate the Daily Average Users (DAU) for each day within the specified date range (from '2023-01-01' to '2023-01-07'), you can use the following SQL query:
sqlSELECT DATE(activity_dt) AS date, COUNT(DISTINCT user_id) AS dau FROM user_activity WHERE DATE(activity_dt) BETWEEN '2023-01-01' AND '2023-01-07' GROUP BY DATE(activity_dt) ORDER BY date;Explanation:
-
DATE(activity_dt):- This extracts the date part from the timestamp
activity_dtso that user activities are grouped by day.
- This extracts the date part from the timestamp
-
COUNT(DISTINCT user_id):- This counts the number of unique users (user_id) for each day, providing the number of distinct users who performed activities on that day.
-
FROM user_activity:
-