Skip to content
Advertisement

How to select a query from mysql database based on a certain condition

I want to answer the following question using the below Mysql tables.

  1. How many days were the user active on avg. (had an action) in the last week?

  2. I want to Display user Avg by day, where ((user action is not 0)/unique day) in the last 7 days.

0 means the user is not active and 1 means active.

I am still a newbie in SQL and here is what I have tried so far. I am not really sure of my answers.

my output

users table

action table

Advertisement

Answer

Average of last week

Since there are 7 days, you can divide the count by 7

For the second problem you can average the sum of action:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement