How to calculate a count of users who did a thing X times during a rolling 7-day period in SQL Server? -


i want calculate count of unique users have posted 5 or more times on course of 7-day rolling period. how do this?

i know how calculate count of users have posted 1 or more times on course of 7-day rolling period. query looks this:

with postsperday (   select cast(creationdate date) [day]   , owneruserid [user]   , count(*) post posts creationdate > '2017-07-01' group    cast(creationdate date)   , owneruserid )  select [day], count(distinct [user]) dailyposters, rolling7daycount postsperday  outer apply (   select count(distinct [user]) rolling7daycount   postsperday ppd   ppd.[day] >= dateadd(dd, -7, postsperday.[day])   , ppd.[day] < postsperday.[day]   ) rolling7daycount  group [day], rolling7daycount order 1 

here @ stack exchange data explorer.

desired results

ideally i'm looking four-column result: day, dailyposters, rolling7daycount, rolling7daycount5xposters. (the sample query returns first 3 columns.)

enter image description here

to clear: i'm hoping count users have posted 5x on course of 7 day period ending on specific date. adding having cte won't give me need.

any performance tips appreciated, too!

in "postsperday" cte change this:

select      cast(creationdate date) [day]     ,owneruserid     ,count(*) post     posts     creationdate > '2017-07-01' group      cast(creationdate date)     ,owneruserid having count(*) >= 5 

i added "having" filter.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -