Anton.Sandbox

1

Please login or register to vote for this query.

(click on this box to dismiss)

Code Golf and Coding Challenges Meta

Q&A about the site for programming puzzle enthusiasts and code golfers

-- At what hour do I add most posts, both questions and answers?
-- UTC (GMT) time zone. The week starts with Sunday.
-- Parameters:
--   UserId
--   HourOrDOW (1 - hours, 2 - day of week, 3 - both)

DECLARE @Choice int = ##HourOrDOW:int?1##

IF @Choice = 1
  SELECT
    DATEPART("hour", CreationDate) AS [Hour you add a post],
    COUNT(*) AS [Number of your posts per hour, UTC (GMT)]
  FROM Posts
  WHERE OwnerUserId = ##UserId##
  GROUP BY DATEPART("hour", CreationDate)
ELSE
IF @Choice = 2
  SELECT
    DATEPART("weekday", CreationDate) AS [Weekday you add a post],
    -- Little hack to show DoW with data.SO graph builder
    DATENAME(dw, CreationDate),
    COUNT(*) AS [Number of your posts per weekday]
  FROM Posts
  WHERE OwnerUserId = ##UserId##
  GROUP BY DATEPART("weekday", CreationDate), DATENAME(dw, CreationDate)
  ORDER BY DATEPART("weekday", CreationDate)
ELSE
IF @Choice = 3
  WITH grouped AS
   (
   SELECT  *, ROUND((CAST(DATEPART("weekday", CreationDate) AS float) + CAST(DATEPART("hour", CreationDate) AS float) / 24), 2) AS cd
   FROM    Posts
   )
  SELECT
    cd AS [Day of week + time],
    COUNT(*) AS [Number of posts]
  FROM grouped
  WHERE OwnerUserId = ##UserId##
  GROUP BY cd
  ORDER BY cd

Enter Parameters

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