Best of Code Review - Best Newcomer (question) category

0

Please login or register to vote for this query.

(click on this box to dismiss)

Code Review

Q&A for peer programmer code reviews

/*
Best of Code Review 2016 - Best Newcomer (question) category
http://meta.codereview.stackexchange.com/questions/7129/best-of-code-review-2016-best-newcomer-question-category
*/
DECLARE @Year AS INT = ##Year:int?2016##

DECLARE @Question INT = 1;
WITH FirstQuestions AS (
SELECT
  MIN(p.Id) AS [PostId],
  DATEPART(YEAR, MIN(p.CreationDate)) AS [YearCreated],
  p.OwnerUserId
  FROM Posts AS p
  WHERE p.PostTypeId = @Question
  GROUP BY p.OwnerUserId
)

SELECT
  p.Id AS [Post Link],
  p.OwnerUserId AS [User Link],
  p.Score,
  p.CreationDate
FROM 
  Posts AS p
  LEFT JOIN FirstQuestions AS fq
    ON fq.OwnerUserId = p.OwnerUserId
WHERE
  DATEPART(YEAR, p.CreationDate) = @Year
  AND fq.YearCreated = @Year
  AND p.ClosedDate IS NULL
  AND p.PostTypeId = @Question
GROUP BY
  p.Id,
  p.OwnerUserId,
  p.Score,
  p.CreationDate
ORDER BY 
  p.OwnerUserId ASC

Enter Parameters

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