Number of tag badges of a user per site


Please login or register to vote for this query.

(click on this box to dismiss)

Craft CMS Meta

Q&A about the site for administrators, end users, developers and designers for Craft CMS

declare @sql nvarchar(max)

create table #result (site sysname
                    , hostname sysname
                    , userid int
                    , gold int
                    , silver int
                    , bronze int
                    , total int                    

select @sql = N'insert into #result' + STRING_AGG(concat(N'
select ''', name ,N''' as site
     , ''',hostname ,N'''
     , sum(case when b.class = 1 then 1 else 0 end) 
     , sum(case when b.class = 2 then 1 else 0 end) 
     , sum(case when b.class = 3 then 1 else 0 end) 
     , count(b.Id)
     from ', quotename(name), N'.dbo.badges b inner join',
       quotename(name), N'.dbo.tags t on inner join',
       quotename(name), N'.dbo.users u on and u.AccountId=',CAST(##AccountId:int?-1## as nvarchar),     
     ' group by',
union all')
from (select convert(nvarchar(max),name) name
    -- , convert(nvarchar(max),sitename)  sitename
    -- , meta
    -- , domain
     ,  concat( 
        -- based on an idea from Glorfindel 
        (case sitename
        WHEN 'Audio' THEN 'video'
        WHEN 'Beer' THEN 'alcohol'
        WHEN 'CogSci' THEN 'psychology'
        WHEN 'Garage' THEN 'mechanics'
        WHEN 'Health' THEN 'medicalsciences'
        WHEN 'Moderators' THEN 'communitybuilding'
        WHEN 'Photography' THEN 'photo'
        WHEN 'Programmers' THEN 'softwareengineering'
        WHEN 'Vegetarian' THEN 'vegetarianism'
        WHEN 'Writers' THEN 'writing'
        WHEN 'Br' THEN 'pt'
        WHEN 'Mathoverflow' THEN concat((meta+'.'), sitename)
        ELSE case when sitename = domain then null else sitename end
        end +'.')
        , (case 
           when sitename <> 'mathoverflow' then (meta+'.') 
           else null 
        , (case 
           when sitename <> 'mathoverflow' then concat((domain + '.'), 'com') 
           else 'net' 
        ) hostname
from (
select name
, case parsename(name,1) 
  when 'Meta' then parsename(name,2)
  else parsename(name,1) 
  end [sitename]
, case parsename(name,1) 
  when 'Meta' then 'meta'
  else null
  end [meta]
  , coalesce(parsename(name,3), parsename(name,2)) [domain]
from sys.databases
where (database_id > 5)
--- and (name like '%.Meta')
) dbs
) dbsall

exec (@sql)

  site, hostname, 
  concat('https://',hostname,'/users/',CAST(userid AS varchar)) AS [User],
  gold, silver, bronze, total
from #result
ORDER BY gold DESC, silver DESC, bronze DESC, total DESC

Enter Parameters

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