Select top N users by upvotes they received for their answers for a given tag located in the specified city or country
Q&A for science fiction and fantasy enthusiasts
-- Top Users by Country -- Created by Mihai Todor (http://stackoverflow.com/users/1174378/mihai-todor) /* SELECT ROW_NUMBER() OVER(ORDER BY Reputation DESC) AS [#], Id AS [User Link], Reputation FROM Users WHERE LOWER(Location) LIKE LOWER('%##CountryName##%') ORDER BY Reputation DESC; */ --SELECT TOP 10 * FROM Tags ORDER BY Count DESC --SELECT * FROM TagSynonyms WHERE LOWER(SourceTagName) LIKE LOWER('%C++%') --ORDER BY Count DESC DECLARE @Tag NVARCHAR(35) = ##Tag:string##; DECLARE @Country NVARCHAR(100) = LTRIM(RTRIM(##Country:string##)); DECLARE @City NVARCHAR(100) = LTRIM(RTRIM(##City:string? ##)); SELECT COUNT(*) FROM Posts AS p INNER JOIN Posts AS a ON a.ParentId = p.id -- answers only INNER JOIN PostTags AS pt ON p.Id = pt.PostId INNER JOIN Tags AS t ON pt.TagId = t.Id INNER JOIN Users AS u ON u.Id = a.OwnerUserId WHERE a.PostTypeId = 2 -- answers only AND t.TagName IN ( SELECT @Tag UNION SELECT SourceTagName FROM TagSynonyms WHERE LOWER(SourceTagName) LIKE LOWER('%' + @Tag + '%') UNION SELECT TargetTagName FROM TagSynonyms WHERE LOWER(TargetTagName) LIKE LOWER('%' + @Tag + '%') ) AND ( LOWER(u.Location) LIKE LOWER('%' + @Country + '%') OR LOWER(u.Location) LIKE LOWER('%' + @City + '%') OR LOWER(u.AboutMe) LIKE LOWER('%' + @Country + '%') OR LOWER(u.AboutMe) LIKE LOWER('%' + @City + '%') ) GROUP BY u.Id ORDER BY SUM(a.Score) DESC