I have a temp_table with user_id and date and I want to find the DAU , WAU and MAU and I am querying this for that where:
DAU – Count of active users for that day
WAU – Count of active users in last 7 days
MAU – Count of active users in last 30 days
where the date is starting from a date that is mentioned here , so there can be no current_date comparison.
dau as (Select casted_date, count(user_id) as dau
from temp table
group by casted_date)
select casted date, dau,
sum(dau) over (order by casted_date rows between -6 preceding and current row) as wau,
sum(dau) over (order by casted_date rows between -29 preceding and current row) as mau
from dau;
but the query is giving me an error like this :
syntax error at or near "-".
PS: I am writing the query in mysql
3
Answers
I don’t know if your query logic be completely correct, but the syntax error you are currently seeing is due to the window function calls. Consider this corrected version:
There is no need to use
-6
to refer to the previous 6 rows, as6 preceding
already means this.First, you have a syntax error, you have
casted date
where you should havecasted_date
and I would not use an alias ofdate
either which happens to be a MySQL keyword without escaping it.You can use
case
–when
to achieve your goal:Tested with this schema:
Fiddle can be found here: http://sqlfiddle.com/#!9/441aaa/10
Xou can use biuild in date functions to parttion the window functions
fiddle