sql server - SQL to determine distinct periods of sequential days of access? -
Asked recently and answered some good answers.
The problem of Jeff revolves around finding users who have (N) use the database table structure as the following day, where they are logged in to a system:
< Pre> id user revenue CreationDate ------ ------ ------------ 750997 12 2009-07-07 18: 42: 20.723 750 998 15 2009-07-07 18: 42: 20.927 751000 19 2009-07-07 18:42: 22.283For further clarity, read more ...
How many different for a user (n) Problem setting day duration Was the secret of
One of the fastest SQL queries that can provide a list of users can be crafted and the number of specific (N) days of the day they
edit : According to a comment below, one has two consecutive days, then one gap, then 4 consecutive days, then an interval, then 8 consecutive days. This will be 3 "different 4 days duration", the 8-day period should be calculated as a two-to-back 4-day period.
My answer appears does not appear ...
I I will try ...
The answer to Rob Farley for the basic question is the easy advantage of consistently adding the number of days. Select ROW_NUMBER () with numberedrows as
(by Partition CreationDate from UserID order) - TheOffice, CreationDate as cast (CreationDate-0.5 as integer) , User ID from tablename, CreationDate, Max CreationDate, Count (*) as NumConsecutiveDays, UserID numberedrows group using UserID, TheOffset
using integer division , Just gives the number of part "by the number of consecutive days" (N) - the period of the day "was fully covered during the entire period ... - 2/4 = 0
- 4/4 = 1
- 8/4 = 2
- 9/4 = 2
- etc, etc.
Then Here is my request for Rob on my answer ...
(I really love, go get EAD explanation, this is motivating thinking)
with numberedrows (UserID, TheOffset) (Select UserID, ROW_NUMBER () (with Partition CreationDate from UserID order) - DateDiff (Day, 0, CreationDate) from Tablename to TheOffset), as ConsecutiveCounts (UserID, ConsecutiveDays) (Select UserID, select UserID, TheOffset as count (*) UserID, as SUM (ConsecutiveDays / @period_length) from the numberedrows group, by ConsecutiveDays UserID, from the ConsecutiveCounts group, distinct_n_day_periods
The only real difference is that I take the results of Rob and then it's somebody Run by other group BY ...
Comments
Post a Comment