Can Data Explorer be used to find total up/down votes cast across all sites?

0

Please login or register to vote for this query.

(click on this box to dismiss)

https://meta.stackexchange.com/questions/135310/

Japanese Language Meta

Q&A about the site for students, teachers, and linguists wanting to discuss the finer points of the Japanese language

declare @sql nvarchar(max)


create table #sites (name sysname
                   , hostname sysname);

create table #result (site sysname
                    , hostname sysname
                    , q integer
                    , a integer
                    , accept integer
                    , up integer
                    , down integer 
                    )

select @sql = N'insert into #sites' + STRING_AGG(concat(N'
select ''', name ,N'''
     , ''', hostname ,N''' 
from ', quotename(name), N'.dbo.users u
where accountid = ##accountid##
'),N' 
union all')
from (select convert(nvarchar(max),name) name
    -- , convert(nvarchar(max),sitename)  sitename
    -- , meta
    -- , domain
     ,  concat( 
        -- based on an idea from Glorfindel 
        -- https://meta.stackexchange.com/a/321181
        (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 
           end) 
        , (case 
           when sitename <> 'mathoverflow' then concat((domain + '.'), 'com') 
           else 'net' 
           end)
        ) 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 not like '%.Meta' or name = 'StackExchange.Meta')
) dbs
) dbsall

exec (@sql)


select @sql='';

select @sql = N'insert into #result' + STRING_AGG(concat(N'
select ''', convert(nvarchar(max),name) ,N'''
     , ''', convert(nvarchar(max),hostname) ,N''' 
     , count(distinct case p.posttypeid when 1 then p.id else null end) Q
     , count(distinct case p.posttypeid when 2 then p.id else null end) A
     , count(distinct case v.votetypeid when 1 then v.id else null end) accept
     , count(distinct case v.votetypeid when 2 then v.id else null end) up
     , count(distinct case v.votetypeid when 3 then v.id else null end) down
from ', quotename(name), N'.dbo.users u
inner join ', quotename(name), N'.dbo.posts p on
  p.owneruserid = u.id
inner join ', quotename(name), N'.dbo.votes v on 
 v.postid = p.id and votetypeid in ( 1,2,3)  
where accountid = ##accountid##
'),N' 
union all')
from #sites

exec (@sql)


select 0 [line]
     , 'Grand Total' as [site]
     , sum(q)  [# questions]
     , sum(a)  [# answers]
     , sum(accept) [accept votes]
     , sum(up) [up votes]
     , sum(down) [down votes] 
from #result
union 
select row_number() over(order by q + a desc) [line]
    , site
    , q
    ,a
    , accept
    , up
    , down
from #result
where q + a + accept + up + down > 0
order by 1

Enter Parameters

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