Created
February 23, 2012 19:40
-
-
Save smoak/1894620 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;WITH base AS | |
( | |
SELECT UserId, | |
DateUtc, | |
ROW_NUMBER() over (partition BY UserId ORDER BY DateUtc) AS rn | |
FROM dbo.UserActivity WHERE DateUtc Between @startDate and @endDate | |
) | |
SELECT b1.UserId, | |
SUM(CASE | |
WHEN | |
DATEDIFF(year,b1.DateUtc,b2.DateUtc) = 0 AND DATEDIFF(day,b1.DateUtc, b2.DateUtc) = 0 AND DATEDIFF(mi, b1.DateUtc, b2.DateUtc) >= @minsForUnique THEN 1 | |
WHEN | |
DATEDIFF(year,b1.DateUtc,b2.DateUtc) = 0 AND DATEDIFF(day,b1.DateUtc, b2.DateUtc) != 0 THEN 1 | |
ELSE 0 END) as [TotalUnique] | |
FROM base b1 | |
INNER JOIN base b2 ON b1.UserId = b2.UserId AND b1.rn != b2.rn | |
GROUP BY b1.UserId |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment