Q&A for Salesforce administrators, implementation experts, developers and anybody in-between
/* BEST TIME TO ASK QUESTIONS The best time here is when the delay between a question and an accepted answer is shortest. On the graph, look for the value on the x-axis that corresponds to the minimum value on y-axis. That's the best time as defined above. As for Dec 2013, the smallest time-to-answer was for questions asked on Saturday at 1pm GMT. The asker had to wait for only 78 minutes. Parameters: - All: Include all users of only one specified by UserId Values: 0 - show stats for one user, 1 - for all users - Frequency: The scale of X-axis Values: 1 - hours, 2 - day of week, 3 - both - MaxTimeDiff: The cut off value for time difference. All time-to-answer above this value will be excluded from the statistics. Default value: 1440 min (24 hours). - UserId: Whose stats to include in calculations. Matters only when All = 0. Values: Your user ID or someone else's. */ DECLARE @Frequency int = ##Frequency:int?3## DECLARE @MaxTimeDiff int = ##MaxTimeDiff:int?1440## DECLARE @All int = ##All:int?1## DECLARE @UsedId int = ##UserId:int## ;WITH tmp AS ( SELECT x1 = q.ViewCount, x2 = q.Score, x3 = DATEDIFF(minute, q.CreationDate, a.CreationDate), Period = CASE @Frequency WHEN 1 THEN DATEPART("hour", q.CreationDate) WHEN 2 THEN DATEPART("weekday", q.CreationDate) WHEN 3 THEN ROUND((CAST(DATEPART("weekday", q.CreationDate) AS numeric) + CAST(DATEPART("hour", q.CreationDate) AS numeric) / 24), 2) END FROM Posts q LEFT OUTER JOIN Posts a ON q.AcceptedAnswerId = a.Id WHERE q.AcceptedAnswerId > 0 AND q.OwnerUserId = CASE WHEN @All = 1 THEN q.OwnerUserId ELSE @UsedId END ) SELECT Period, AVG(x1) as m_views, ROUND(STDEV(CAST(x1 AS FLOAT)), 2) as sd_views, AVG(x2) as m_score, ROUND(STDEV(CAST(x2 AS FLOAT)), 2) as sd_scores, AVG(x3) as m_td, ROUND(STDEV(CAST(x3 AS FLOAT)), 2) as sd_td FROM tmp GROUP BY Period ORDER BY Period