Ratio of questions with specific tag asked per time period

0

Please login or register to vote for this query.

(click on this box to dismiss)

Movies and TV

Q&A for movie and tv enthusiasts

DECLARE @BeginDate DATETIME = (SELECT min(CreationDate) FROM PostHistory);
DECLARE @EndDate DATETIME = (SELECT max(CreationDate) FROM PostHistory);
DECLARE @Pattern VARCHAR(25) = ##TagPattern:string?%identify-this-%##;
DECLARE @Period INT = ##TimePeriod:int?7##;

WITH Dates AS
(
  SELECT @BeginDate AS [DateValue]
  UNION ALL
  SELECT DateValue + @Period FROM Dates WHERE DateValue + @Period < @EndDate
)

SELECT D.DateValue AS [Date],
/*
  (SELECT count(*) FROM Posts P WHERE P.PostTypeId = 1 AND 
    P.CreationDate BETWEEN D.DateValue AND DateValue + @Period) AS [Total],
  (SELECT count(*) FROM Posts P WHERE P.Tags LIKE @Pattern AND 
    P.CreationDate BETWEEN D.DateValue AND DateValue + @Period) AS [ID],
  (SELECT count(*) FROM Posts P WHERE P.Tags NOT LIKE @Pattern AND 
    P.CreationDate BETWEEN D.DateValue AND DateValue + @Period) AS [Non-ID]
*/
  (CASE WHEN EXISTS (SELECT * FROM Posts P WHERE P.PostTypeId = 1 AND 
    P.CreationDate BETWEEN D.DateValue AND DateValue + @Period)
   THEN
    100.0 * (SELECT count(*) FROM Posts P WHERE P.Tags LIKE @Pattern AND 
      P.CreationDate BETWEEN D.DateValue AND DateValue + @Period) / 
    (SELECT count(*) FROM Posts P WHERE P.PostTypeId = 1 AND 
      P.CreationDate BETWEEN D.DateValue AND DateValue + @Period) 
   ELSE 0 END) AS [ID %]
FROM
  Dates AS D
ORDER BY D.DateValue ASC
OPTION (MAXRECURSION 0)

Enter Parameters

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