I have a case study which is similar to the one discussed here: Count active users using login timestamp in MySQL
However I would like to solve it in SQL Server language (not mySQL) and I have 2 slightly different requests: Given the table containing User_id and login_timestamp calculate:
1) the Ratio of people who logged every day in the last week 2) the monthly frequency of access of people, given their latest login (e.g. assuming their last login was yesterday, what has been the monthly frequency starting from yesterday and looking 1 month back?)
For the first question I have the answer, but I would like to ask if someone can validate it for me?
select sum(A.access)/count(A.access) as ratio_weekly_access
from
(select user_id,
case when
count(distinct format(timestamp_login,'YYYY-MM-DD')) =7 then 1
else 0 end as access
from xxx
where FORMAT(timestamp_login,'YYYY-MM-DD') between dateadd(day,-7, cast(getdate() as date)) and cast(getdate() as date)
group by user_id
) A
Thank you in advance for your help!