Most active voters across the whole network

2

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for professional and enthusiast programmers

DECLARE @output TABLE (
  DbName VARCHAR(max),
  UserName VARCHAR(max),
  VoteNo INT,
  Reputation INT,
  AccountId INT);

DECLARE @query VARCHAR(MAX);

DECLARE dbcursor CURSOR FOR
SELECT name
FROM sysdatabases
WHERE name NOT IN ('master', 'tempdb', 'model' ,'msdb', 'Data.StackExchange');

DECLARE @dbname VARCHAR(max);

OPEN dbcursor;
FETCH NEXT FROM dbcursor INTO @dbname;

WHILE @@FETCH_STATUS = 0
BEGIN
  SELECT @query = '
    SELECT DISTINCT
      ''' + @dbname + ''' AS DbName,
      MAX(Users.DisplayName) AS UserName,
      COUNT(*) + MIN(Users.UpVotes) + MIN(Users.DownVotes) AS VoteNo,
      MIN(Users.Reputation),
      MIN(Users.AccountId)
    FROM [' + @dbname + '].dbo.Users
    LEFT JOIN[' + @dbname + '].dbo.Votes ON Users.Id = Votes.UserId
    WHERE Users.Reputation >= 15
    GROUP BY Users.Id;';
  INSERT INTO @output EXEC (@query);
  FETCH NEXT FROM dbcursor INTO @dbname;
END

CLOSE dbcursor;

WITH CombinedVotes AS (
  SELECT
    MIN(UserName) AS UserName,
    AccountId,
    MAX(Reputation) AS MaxRep,
    SUM(Reputation) AS Reputation,
    SUM(VoteNo) AS VoteNo
  FROM @output
  GROUP BY AccountId
)
SELECT
  DENSE_RANK() OVER (ORDER BY VoteNo DESC) AS Rank,
  'http://stackexchange.com/users/'
    + CAST(AccountId AS VARCHAR(30))
    + '?tab=accounts|' + UserName AS 'SE Account',
  AccountId,
  VoteNo,
  MaxRep,
  Reputation
FROM CombinedVotes
ORDER BY VoteNo DESC, AccountId;

Enter Parameters

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