Questions created in quarter-range

0

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for professional and enthusiast programmers

-- Quarter: Enter the quarter of the year. Must be a number from 1-4.
-- Year: Enter the year, must a number, e.g. 2015.

DECLARE @Quarter int = ##Quarter:int?1##, @Year int = ##Year:int?2015##;

DECLARE @StartDate date =
  CASE @Quarter
    WHEN 1 THEN CAST(CAST(@Year AS varchar) + '-01-01' AS date)
    WHEN 2 THEN CAST(CAST(@Year AS varchar) + '-04-01' AS date)
    WHEN 3 THEN CAST(CAST(@Year AS varchar) + '-07-01' AS date)
    WHEN 4 THEN CAST(CAST(@Year AS varchar) + '-10-01' AS date)
    ELSE CAST('2015-01-01' AS date)
   END;
   
DECLARE @EndDate date=
  CASE @Quarter
    WHEN 1 THEN CAST(CAST(@Year AS varchar) + '-03-31' AS date)
    WHEN 2 THEN CAST(CAST(@Year AS varchar) + '-06-30' AS date)
    WHEN 3 THEN CAST(CAST(@Year AS varchar) + '-09-30' AS date)
    WHEN 4 THEN CAST(CAST(@Year AS varchar) + '-12-31' AS date)
    ELSE CAST(CAST(@Year AS varchar) + '-12-31' AS date)
   END;

SELECT
  Posts.Id AS [Post Link],
  Posts.CreationDate AS [Date Created],
  Users.Id AS [User Link],
  Posts.ViewCount AS Views,
  Posts.Score,
  Posts.FavoriteCount AS [Favorite Count]
FROM Posts
  INNER JOIN Users
    ON Posts.OwnerUserId = Users.Id
WHERE Posts.CreationDate BETWEEN @StartDate AND @EndDate
  -- Questions only
  AND PostTypeId = 1
ORDER BY Posts.CreationDate;

Enter Parameters

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