Networkwide questions per account, ordered by view count

0

Please login or register to vote for this query.

(click on this box to dismiss)

Genealogy and Family History Meta

Q&A about the site for expert genealogists and people interested in genealogy or family history

-- result table, don't rename and keep the site column
create table #results (site nvarchar(250), id int, title nvarchar(250)
                       , viewcount 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
N'
select ''' + name + '''
     , p.id, p.title, p.viewcount
from ' + quotename(name) + '.dbo.posts as p
inner join ' + quotename(name) + '.dbo.users as u on p.owneruserid = u.id
where p.posttypeid = 1 and u.accountid = ' + CAST(##AccountId:int## AS nvarchar) + ''
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
       -- URL builder from Lamak http://stackoverflow.com/a/38129701
      'http://' 
      + case PARSENAME(site,1)
        when 'audio' then 'avp'
        else PARSENAME(site,1)
        end 
      + coalesce('.' 
        + case PARSENAME(site,2)
          when 'audio' then 'avp'
          else PARSENAME(site,2)
          end , '')
      + IIF(patindex('%.MathOverflow%',site) > 0
      , ''
      , coalesce('.' + PARSENAME(site,3), '')
      )
      + IIF(patindex('%.MathOverflow%',site) > 0
      , '.net'
      , '.com'
      ) 
      + '/q/'
      + CAST(id AS nvarchar) 
      + '|'
      + title
      -- rest of columns
      , viewcount
from #results
order by viewcount desc

drop table #results

Enter Parameters

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