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

;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