All the distinct days on which I created posts

0

Please login or register to vote for this query.

(click on this box to dismiss)

Lifehacks

Q&A for people looking to bypass life's everyday problems with simple tricks

WITH 
  groups(userid, date, grp) AS (
    SELECT DISTINCT
      OwnerUserId,
      CAST(CreationDate AS DATE),
      dateadd(day, 
        -DENSE_RANK() OVER (PARTITION BY OwnerUserId ORDER BY CAST(CreationDate AS DATE)), 
        CAST(CreationDate AS DATE)) AS grp
    FROM Posts
    where OwnerUserId = ##UserId##
  ),
  max_consecutive_dates AS (
    SELECT 
      userid,
      COUNT(*) AS consecutiveDates,
      MIN(date) AS minDate,
      MAX(date) AS maxDate,
      ROW_NUMBER() OVER (PARTITION BY userid ORDER BY COUNT(*) DESC) AS rank
    FROM groups
    GROUP BY userid, grp
  )
SELECT 
  userid,
 consecutiveDates AS maxConsecutiveDates,
  minDate,
  maxDate
FROM max_consecutive_dates
where rank=1
order by 2 Desc;

Enter Parameters

Options:
Switch to meta site
loading Hold tight while we fetch your results