;WITH Points AS ( SELECT CreationDate AS [Date], SUM(Vote...

0

Please login or register to vote for this query.

(click on this box to dismiss)

Meta Stack Exchange

Q&A about the Stack Exchange Network

;WITH Points AS (
  SELECT
    CreationDate AS [Date],
    SUM(Vote) AS [Votes],
    CASE WHEN SUM(Edit) > 0 THEN 1 END AS [Edit]
  FROM (
    SELECT
      0 AS Vote,
      1 AS Edit,
      DATEADD(DAY, DATEDIFF(DAY, 0, CreationDate), 0) AS CreationDate
    FROM
      PostHistory WHERE PostHistoryTypeId IN (4, 5, 6) AND
      PostId = ##PostId##
  UNION
    SELECT
      CASE WHEN VoteTypeId = 2 THEN 1 ELSE -1 END AS Vote,
      0 AS Edit,
      CreationDate
    FROM
      Votes
    WHERE
      VoteTypeId IN (2, 3) AND
      PostId = ##PostId##
  ) AS History
  GROUP BY CreationDate
)

SELECT
  [Date],
  (SELECT SUM(totals.Votes) FROM Points totals WHERE totals.[Date] <= p.[Date]) AS Votes,
  Edit
FROM
  Points p
ORDER BY
  [Date] DESC

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results
:records returned in :time ms:cached