Election Votes per Hour with Running Total

0

Please login or register to vote for this query.

(click on this box to dismiss)

Android Enthusiasts

Q&A for enthusiasts and power users of the Android operating system

WITH
  Votes AS (
    SELECT
      DATEPART(HOUR, b.Date) AS VoteHour,
      CAST(b.Date AS DATE) AS VoteDate,
      DATEPART(YEAR, b.Date) AS ElectionYear
    FROM Badges b
    WHERE b.Name = 'Constituent'
  ),
  VotesPerHour AS (
    SELECT
      DATETIMEFROMPARTS(YEAR(VoteDate), MONTH(VoteDate), DAY(VoteDate), VoteHour, 0, 0, 0) AS VoteDateHour,
      ElectionYear
    FROM Votes
  ),
  BadgesPerHour AS (
    SELECT
      ElectionYear,
      VoteDateHour,
      COUNT(*) AS BadgeCount
    FROM VotesPerHour
    GROUP BY
      ElectionYear,
      VoteDateHour
  )
SELECT
  ElectionYear,
  VoteDateHour,
  BadgeCount,
  SUM(BadgeCount) OVER (PARTITION BY ElectionYear ORDER BY VoteDateHour) AS RunningTotal
FROM BadgesPerHour
ORDER BY
  ElectionYear,
  VoteDateHour

Enter Parameters

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