In the following:
(running a query on the Stack Exchange Data Explorer, which uses an SQL Azure implementation of OData. The FAQ says it supports most of the TSQL commands):
DECLARE @MinPosts int = ##MinNumberOfPosts##
SELECT
Id AS [User Link],
Reputation,
(SELECT COUNT(*)
FROM posts
WHERE posts.OwnerUserId = Users.Id
) AS [# Posts],
Reputation /
(SELECT COUNT(*)
FROM posts
WHERE posts.OwnerUserId = Users.Id
) AS [Rep Per Post]
FROM Users
WHERE (SELECT COUNT(*)
FROM posts
WHERE posts.OwnerUserId = Users.Id
) > @MinPosts
ORDER BY [Rep Per Post] DESC
I write out:
(SELECT COUNT(*)
FROM posts
WHERE posts.OwnerUserId = Users.Id)
three times. How can I create a variable or a function for the above snippet of code?
I tried using the column alias [# Posts]
, but that didn't work.
I found these posts on reusing a calculated filed name for further calculation, which seems like what I want, but I can't figure out how to apply it.