Q&A for professional and enthusiast programmers
DECLARE @Tag1 nvarchar(max) = 'angularjs' DECLARE @Tag2 nvarchar(max) = 'knockout.js' DECLARE @1MonthsAgo datetime = DATEADD(month, -1, GETDATE()) DECLARE @2MonthsAgo datetime = DATEADD(month, -2, GETDATE()) DECLARE @3MonthsAgo datetime = DATEADD(month, -3, GETDATE()) DECLARE @4MonthsAgo datetime = DATEADD(month, -4, GETDATE()) DECLARE @5MonthsAgo datetime = DATEADD(month, -5, GETDATE()) DECLARE @6MonthsAgo datetime = DATEADD(month, -6, GETDATE()) DECLARE @Tag1Count1Months decimal (12,6), @Tag1Count1MonthsText nvarchar(max), @t1g1 decimal (12,6), @t1g1Text nvarchar(max) DECLARE @Tag1Count2Months decimal (12,6), @Tag1Count2MonthsText nvarchar(max), @t1g2 decimal (12,6), @t1g2Text nvarchar(max) DECLARE @Tag1Count3Months decimal (12,6), @Tag1Count3MonthsText nvarchar(max), @t1g3 decimal (12,6), @t1g3Text nvarchar(max) DECLARE @Tag1Count4Months decimal (12,6), @Tag1Count4MonthsText nvarchar(max), @t1g4 decimal (12,6), @t1g4Text nvarchar(max) DECLARE @Tag1Count5Months decimal (12,6), @Tag1Count5MonthsText nvarchar(max), @t1g5 decimal (12,6), @t1g5Text nvarchar(max) DECLARE @Tag1Count6Months decimal (12,6), @Tag1Count6MonthsText nvarchar(max) DECLARE @Tag2Count1Months decimal (12,6), @Tag2Count1MonthsText nvarchar(max), @t2g1 decimal (12,6), @t2g1Text nvarchar(max) DECLARE @Tag2Count2Months decimal (12,6), @Tag2Count2MonthsText nvarchar(max), @t2g2 decimal (12,6), @t2g2Text nvarchar(max) DECLARE @Tag2Count3Months decimal (12,6), @Tag2Count3MonthsText nvarchar(max), @t2g3 decimal (12,6), @t2g3Text nvarchar(max) DECLARE @Tag2Count4Months decimal (12,6), @Tag2Count4MonthsText nvarchar(max), @t2g4 decimal (12,6), @t2g4Text nvarchar(max) DECLARE @Tag2Count5Months decimal (12,6), @Tag2Count5MonthsText nvarchar(max), @t2g5 decimal (12,6), @t2g5Text nvarchar(max) DECLARE @Tag2Count6Months decimal (12,6), @Tag2Count6MonthsText nvarchar(max) -- Tag 1 Activity SET @Tag1Count1Months = (SELECT COUNT(*) AS Posts FROM Tags INNER JOIN PostTags ON PostTags.TagId = Tags.id INNER JOIN Posts ON Posts.ParentId = PostTags.PostId where tagname = @Tag1 AND posts.creationdate > @1MonthsAgo group by tagname); SET @Tag1Count2Months = (SELECT COUNT(*) AS Posts FROM Tags INNER JOIN PostTags ON PostTags.TagId = Tags.id INNER JOIN Posts ON Posts.ParentId = PostTags.PostId where tagname = @Tag1 AND posts.creationdate > @2MonthsAgo and posts.creationdate < @1MonthsAgo group by tagname); SET @Tag1Count3Months = (SELECT COUNT(*) AS Posts FROM Tags INNER JOIN PostTags ON PostTags.TagId = Tags.id INNER JOIN Posts ON Posts.ParentId = PostTags.PostId where tagname = @Tag1 AND posts.creationdate > @3MonthsAgo and posts.creationdate < @2MonthsAgo group by tagname); SET @Tag1Count4Months = (SELECT COUNT(*) AS Posts FROM Tags INNER JOIN PostTags ON PostTags.TagId = Tags.id INNER JOIN Posts ON Posts.ParentId = PostTags.PostId where tagname = @Tag1 AND posts.creationdate > @4MonthsAgo and posts.creationdate < @5MonthsAgo group by tagname); SET @Tag1Count5Months = (SELECT COUNT(*) AS Posts FROM Tags INNER JOIN PostTags ON PostTags.TagId = Tags.id INNER JOIN Posts ON Posts.ParentId = PostTags.PostId where tagname = @Tag1 AND posts.creationdate > @5MonthsAgo and posts.creationdate < @4MonthsAgo group by tagname); SET @Tag1Count6Months = (SELECT COUNT(*) AS Posts FROM Tags INNER JOIN PostTags ON PostTags.TagId = Tags.id INNER JOIN Posts ON Posts.ParentId = PostTags.PostId where tagname = @Tag1 AND posts.creationdate > @6MonthsAgo and posts.creationdate < @5MonthsAgo group by tagname); SET @Tag1Count6MonthsText = convert(varchar,convert(int,@Tag1Count6Months)) SET @t1g1 = (@Tag1Count6Months-@Tag1Count5Months) / @Tag1Count6Months * 100.0; SET @t1g1Text = convert(varchar,convert(decimal(5,2),@t1g1)) + '%' SET @Tag1Count5MonthsText = convert(varchar,convert(int,@Tag1Count5Months)) SET @t1g2 = (@Tag1Count5Months-@Tag1Count5Months) / @Tag1Count5Months * 100.0; SET @t1g2Text = convert(varchar,convert(decimal(5,2),@t1g2)) + '%' SET @Tag1Count4MonthsText = convert(varchar,convert(int,@Tag1Count4Months)) SET @t1g3 = (@Tag1Count4Months-@Tag1Count3Months) / @Tag1Count4Months * 100.0; SET @t1g3Text = convert(varchar,convert(decimal(5,2),@t1g3)) + '%' SET @Tag1Count3MonthsText = convert(varchar,convert(int,@Tag1Count3Months)) SET @t1g4 = (@Tag1Count3Months-@Tag1Count2Months) / @Tag1Count2Months * 100.0; SET @t1g4Text = convert(varchar,convert(decimal(5,2),@t1g4)) + '%' SET @Tag1Count2MonthsText = convert(varchar,convert(int,@Tag1Count2Months)) SET @t1g5 = (@Tag1Count2Months-@Tag1Count1Months) / @Tag1Count2Months * 100.0; SET @t1g2Text = convert(varchar,convert(decimal(5,2),@t1g5)) + '%' SET @Tag1Count1MonthsText = convert(varchar,convert(int,@Tag1Count1Months)) -- Tag 2 Activity SET @Tag2Count4Months = (SELECT COUNT(*) AS Posts FROM Tags INNER JOIN PostTags ON PostTags.TagId = Tags.id INNER JOIN Posts ON Posts.ParentId = PostTags.PostId where tagname = @Tag2 AND posts.creationdate > @1MonthsAgo group by tagname); SET @Tag2Count3Months = (SELECT COUNT(*) AS Posts FROM Tags INNER JOIN PostTags ON PostTags.TagId = Tags.id INNER JOIN Posts ON Posts.ParentId = PostTags.PostId where tagname = @Tag2 AND posts.creationdate > @2MonthsAgo and posts.creationdate < @1MonthsAgo group by tagname); SET @Tag2Count2Months = (SELECT COUNT(*) AS Posts FROM Tags INNER JOIN PostTags ON PostTags.TagId = Tags.id INNER JOIN Posts ON Posts.ParentId = PostTags.PostId where tagname = @Tag2 AND posts.creationdate > @3MonthsAgo and posts.creationdate < @2MonthsAgo group by tagname); SET @Tag2Count1Months = (SELECT COUNT(*) AS Posts FROM Tags INNER JOIN PostTags ON PostTags.TagId = Tags.id INNER JOIN Posts ON Posts.ParentId = PostTags.PostId where tagname = @Tag2 AND posts.creationdate > @4MonthsAgo and posts.creationdate < @3MonthsAgo group by tagname); SET @Tag2Count4MonthsText = convert(varchar,convert(int,@Tag2Count4Months)) SET @t2g1 = (@Tag2Count3Months-@Tag2Count4Months) / @Tag2Count3Months * 100.0; SET @t2g1Text = convert(varchar,convert(decimal(5,2),@t2g1)) + '%' SET @Tag2Count3MonthsText = convert(varchar,convert(int,@Tag2Count3Months)) SET @t2g2 = (@Tag2Count2Months-@Tag2Count3Months) / @Tag2Count2Months * 100.0; SET @t2g2Text = convert(varchar,convert(decimal(5,2),@t2g2)) + '%' SET @Tag2Count2MonthsText = convert(varchar,convert(int,@Tag2Count2Months)) SET @t2g3 = (@Tag2Count1Months-@Tag2Count2Months) / @Tag2Count1Months * 100.0; SET @t2g3Text = convert(varchar,convert(decimal(5,2),@t2g3)) + '%' SET @Tag2Count1MonthsText = convert(varchar,convert(int,@Tag2Count1Months)) SELECT 'Tag', 'Posts', 'Change', 'Date' UNION ALL SELECT @Tag1, @Tag1Count6MonthsText, '', '6 months ago' UNION ALL SELECT @Tag1, @Tag1Count5MonthsText, @t1g1Text, '3 months ago' UNION ALL SELECT @Tag1, @Tag1Count4MonthsText, @t1g2Text, '3 months ago' UNION ALL SELECT @Tag1, @Tag1Count3MonthsText, @t1g3Text, '3 months ago' UNION ALL SELECT @Tag1, @Tag1Count2MonthsText, @t1g4Text, '2 months ago' UNION ALL SELECT @Tag1, @Tag1Count1MonthsText, @t1g5Text, 'last month' UNION ALL SELECT '', '', '', '' UNION ALL SELECT @Tag2, @Tag2Count4MonthsText, '', '4 months ago' UNION ALL SELECT @Tag2, @Tag2Count3MonthsText, @t2g1Text, '3 months ago' UNION ALL SELECT @Tag2, @Tag2Count2MonthsText, @t2g2Text, '2 months ago' UNION ALL SELECT @Tag2, @Tag2Count1MonthsText, @t2g3Text, 'last month'