I'm trying to calculate the DAU average for each country for a time period of 1 month. The job of the query is to:
- identify unique users
- find all users who logged in during last month
- group them into individual days
- segment them into their respective countries
- count the average for each country.
So far I've managed steps 1, 2, 3 and 4, but the last one is proving to be tricky.
The query is supposed to first calculate the subquery where it calculates how many active users opened the app in the last month and then group them into days and countries. After this, it should calculate the average DAU for each country using all 30 days data it has calculated in the subquery. The result would then be a list of countries and their average DAU.
query so far looks like this:
SELECT Country, AVG(User_ID)
FROM usersession
WHERE User_ID IN
(SELECT count(distinct us.User_ID)
FROM usersession us
WHERE Opened > current_timestamp - interval 1 month
GROUP BY DAY(Opened), Country)
GROUP BY Country ORDER BY Country;
The subquery does steps 1,2,3,4 but the secondary query outside the subquery isn't just working as intended.
Table is as follows (just a short example of the relevant information):
ID | UserID | Opened | Country
-----------------------------------------------
233231 1 2017-11-20 08:00:00 NA
223214 2 2017-11-20 08:53:00 DK
Expected result (around 230 countries total):
Country | Average
------------------
NA 150354
DK 60345
FI 50242
Actual result:
+---------+--------------+
| Country | AVG(User_ID) |
+---------+--------------+
| NULL | 804397.7297 |
| | 746046.7500 |
| BR | 893252.0000 |
| GB | 935599.0000 |
| RU | 993311.0000 |
| US | 735568.0000 |
+---------+--------------+
distinct
is not a function! Remove those redundant parentheses to make things clearer, i.e. docount(distinct us.User_ID)
instead. – jarlhUser_ID IN (count of something GROUP BY something)
make sense? Please edit your question to clarify. – O. Jones