Counts of edits adding/removing given tag

1

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow en español

Preguntas y respuestas para programadores y profesionales de la informática

--this query misses some tags additions and removals
--if the tag was added and removed in the same posts several times
--in deleted posts (they are not present in the PostHistoryTable)
;  with firstwithtag_cte as
(
SELECT
p.Id AS pid, Min(ph1.Id) AS phWithId, Min(ph1.Id) as MaxWithTag
FROM PostHistory ph1 INNER JOIN Posts p ON ph1.PostId=p.Id
WHERE ph1.PostHistoryTypeId in (3,6,9)
  and ph1.Text Like '%<##tagName##>%'
GROUP BY p.Id
), firstwithouttag_cte as
(SELECT
  pid, phWithId, MIN(ph2.Id) as phWithoutId
--phWithoutId is the PostHistory.Id for the LAST removal of the tag from the post with Post.Id=pid
FROM firstwithtag_cte INNER JOIN PostHistory ph2 ON ph2.PostId=pid
  WHERE (ph2.Id>MaxWithTag)
   AND ph2.PostHistoryTypeId in (3,6,9)
   AND NOT(ph2.Text Like '%<##tagName##>%')
GROUP BY pid, phWithId
), datesaddedremoved_cte as
(
SELECT pid, ph1.CreationDate as FirstAddedDate, ph2.CreationDate as LastRemovedDate
FROM firstwithouttag_cte INNER JOIN PostHistory ph1 ON ph1.Id=phWithId
  INNER JOIN PostHistory ph2 ON ph2.Id=phWithoutId
)

SELECT
  d1.pid AS [Post Link], d1.LastRemovedDate,
  (SELECT COUNT(*) FROM datesaddedremoved_cte d2 WHERE d2.FirstAddedDate<=d1.LastRemovedDate) AS CountAddedBefore,
  (SELECT COUNT(*) FROM datesaddedremoved_cte d2 WHERE d2.LastRemovedDate<=d1.LastRemovedDate) AS CountRemovedBefore
FROM datesaddedremoved_cte d1
ORDER BY d1.pid

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results