Shows the trend for your favourite tag
Q&A for those who study, teach, research and apply economics and econometrics
DECLARE @TagName varchar(255) = '##TagName##'; DECLARE @PostCountByMonth TABLE ( [date] datetime, [totalCount] int, [##TagName##Count] int, UNIQUE CLUSTERED ([date]) ); INSERT INTO @PostCountByMonth SELECT dateadd(month, DATEDIFF(month, '', p.CreationDate), '') [date], count(*) [totalCount], count(p.[IsTag]) [##TagName##Count] FROM ( SELECT Posts.CreationDate, (case when Posts.Tags LIKE '%<##TagName##>%' then 1 else null end) [IsTag] FROM Posts ) p GROUP BY DATEDIFF(month, '', p.CreationDate) ORDER BY DATEDIFF(month, '', p.CreationDate); SELECT p1.[date], sum(p2.[totalCount]) [totalCountTrend], sum(p2.[##TagName##Count]) [##TagName##CountTrend], sum(p2.[##TagName##Count] * 1000) / sum(p2.[totalCount]) [ratio] FROM @PostCountByMonth p1 INNER JOIN @PostCountByMonth p2 ON p1.[date] >= p2.[date] GROUP BY p1.[date]