Average views per question in a given period


Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for professional and enthusiast programmers

/* How many views per question appears in a given week
For instance, is SE more active during the Christmas–NY season
or the community do something else?

- All: Include all users or only one specified by UserId
    Values: 0 - show stats for one user, 1 - for all users
- UserId: Whose stats to include in calculations. Matters only when All = 0.
    Values: Your user ID or someone else's.
- YearsBack: By default, only one previous year is taken (YearsBack = 1).
- Month, DayStart, DayEnd: The period you're interested in.

DECLARE @All int = ##All:int?1##
DECLARE @UsedId int = ##UserId:int##
DECLARE @YearsBack int = ##YearsBack:int?1##
DECLARE @Month int = ##Month:int?12##
DECLARE @DayStart int = ##DayStart:int?20##
DECLARE @DayEnd int = ##DayEnd:int?31##

;WITH tmp AS
  SELECT q.ViewCount, DATEPART("day", q.CreationDate) as CreationDay
  FROM Posts q
  WHERE q.OwnerUserId = CASE WHEN @All = 1 THEN q.OwnerUserId ELSE @UsedId END
    AND q.CreationDate > DATEADD("year", -@YearsBack-1, CURRENT_TIMESTAMP)
    AND q.CreationDate < DATEADD("year", -1, CURRENT_TIMESTAMP)
    AND q.PostTypeId = 1
    AND DATEPART("month", q.CreationDate) = @Month
    AND DATEPART("day", q.CreationDate) >= @DayStart
    AND DATEPART("day", q.CreationDate) <= @DayEnd
  SELECT CreationDay,
  CAST(SUM(ViewCount) AS FLOAT)/CAST(COUNT(*) AS FLOAT) AS [Views per question]
FROM tmp
GROUP BY CreationDay
ORDER BY CreationDay

Enter Parameters

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