Number of comments by a specific user depending on hour in a day

1

Please login or register to vote for this query.

(click on this box to dismiss)

Physical Fitness Meta

Q&A about the site for physical fitness professionals, athletes, trainers, and those providing health-related needs

/*
The usage of Period to switch between daily hours/hour and day in week is borrowed from https://data.stackexchange.com/math/query/155865/best-time-to-ask-questions-on-stackexchange-more-likely-to-get-an-answer
https://meta.stackexchange.com/questions/213603/question-and-answer-activity-on-stack-exchange-interactive-and-customized-editi
*/
DECLARE @Frequency int = ##Frequency:int?3##
DECLARE @UserId int = ##UserId:int##

;WITH tmp AS
(SELECT Id,
  Period = CASE @Frequency
    WHEN 1 THEN DATEPART("hour", CreationDate)
    WHEN 2 THEN DATEPART("weekday", CreationDate)
    WHEN 3 THEN ROUND((CAST(DATEPART("weekday", CreationDate) AS numeric)
    + CAST(DATEPART("hour", CreationDate) AS numeric) / 24), 2) END
FROM Comments
WHERE UserId = @UserId)
SELECT Period, COUNT(*) AS number,
ROUND((COUNT(*)*100.0/(SELECT COUNT(*) FROM tmp)),2) AS percentage
FROM tmp
GROUP BY Period
ORDER BY Period

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results