Top users by total reputation networkwide


Please login or register to vote for this query.

(click on this box to dismiss)

Academia Meta

Q&A about the site for academics and those enrolled in higher education

-- result table, don't rename and keep the site column
create table #results (site nvarchar(250), accountid int,
  displayname nvarchar(40), reputation int);

declare @sql nvarchar(max) = ''   -- holds build up sql string

-- build one biq union sql, for each db
select @sql = @sql 
+ iif( len(@sql) > 1 
     , 'union all'
     , 'insert into #results'
) +
-- here goes the per site query, fully qualify the database objects
select ''' + name + ''', accountid, displayname, reputation
from ' + quotename(name) + '.dbo.users
from sys.databases
where database_id > 5
  and (name not like '%.Meta' or name = 'StackExchange.Meta')

--print @sql

-- execute it
exec (@sql)

-- show results
select rank() over (order by sum(reputation) desc) AS 'Rank',
  '' + cast(accountid as nvarchar)
  + '|' + max(displayname) AS 'User',
  sum(reputation) AS 'Total reputation'
from #results
group by accountid
order by sum(reputation) desc

drop table #results

Enter Parameters

Switch to main site
loading Hold tight while we fetch your results