Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So - I'm going to start with my query from the last question, which listed each run of consecutive days. Then I'm going to group that by userid and NumConsecutiveDays, to count how many runs of days there are for those users.</p> <pre><code>with numberedrows as ( select row_number() over (partition by UserID order by CreationDate) - cast(CreationDate-0.5 as int) as TheOffset, CreationDate, UserID from tablename ) , runsOfDay as ( select min(CreationDate), max(CreationDate), count(*) as NumConsecutiveDays, UserID from numberedrows group by UserID, TheOffset ) select UserID, NumConsecutiveDays, count(*) as NumOfRuns from runsOfDays group by UserID, NumConsecutiveDays ; </code></pre> <p>And of course, if you want to filter this to only consider runs of a certain length, then put "where NumConsecutiveDays >= @days" in the last query.</p> <p>Now, if you want to count a run of 16 days as three 5-day runs, then each run will count as NumConsecutiveDays / @runlength of these (which will round down for each integer). So now instead of just counting how many there are of each, use SUM instead. You could use the query above and use SUM(NumOfRuns * NumConsecutiveDays / @runlength), but if you understand the logic, then the query below is a bit easier.</p> <pre><code>with numberedrows as ( select row_number() over (partition by UserID order by CreationDate) - cast(CreationDate-0.5 as int) as TheOffset, CreationDate, UserID from tablename ) , runsOfDay as ( select min(CreationDate), max(CreationDate), count(*) as NumConsecutiveDays, UserID from numberedrows group by UserID, TheOffset ) select UserID, sum(NumConsecutiveDays / @runlength) as NumOfRuns from runsOfDays where NumConsecutiveDays &gt;= @runlength group by UserID ; </code></pre> <p>Hope this helps,</p> <p>Rob</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload