0 for userid means all users
Q&A for SharePoint enthusiasts
-- Regional User Rank -- 0 for userid means all users DECLARE @UserId int = ##UserId## ;WITH regionalranking AS (SELECT u.reputation, u.displayname, u.location, u.id, Rank() OVER (PARTITION BY u.location ORDER BY u.reputation DESC) rk FROM users u) SELECT rk, reputation, location, displayname FROM regionalranking WHERE ( id = @UserId OR @UserId = 0 ) ORDER BY location, rk