How many times have been the tag added/removed


Please login or register to vote for this query.

(click on this box to dismiss)

History of Science and Math

Q&A for people interested in the history and origins of science and mathematics

--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
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##>%'
), firstwithouttag_cte as
  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
), counts_cte AS
(SELECT AS pid, d1.LastRemovedDate AS rDate,
  (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)

  pid AS [Post Link], 
  url = 'site://posts/' + CAST(pid AS varchar)+'/revisions',
  rDate AS [DateRemoved]
FROM counts_cte
WHERE (CountAddedBefore=CountRemovedBefore)

Enter Parameters

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