Compare answer volume in two periods, network wide

0

Please login or register to vote for this query.

(click on this box to dismiss)

Android Enthusiasts

Q&A for enthusiasts and power users of the Android operating system

-- start create url from dbname
IF OBJECT_ID ( '#siteurl', 'P' ) IS NOT NULL 
    DROP PROCEDURE #siteurl;
GO
create procedure #siteurl
   @dbname nvarchar(250),
   @res nvarchar(250) OUT
   as
   begin
      -- we have three sites that have borked DNS entries
      set @dbname = (case @dbname
      when 'StackExchange.Audio' then 'StackExchange.Avp'
      when 'StackExchange.Audio.Meta' then 'StackExchange.Avp.Meta'
      when 'StackExchange.Mathoverflow.Meta' then 'net.Mathoverflow.meta'
      else @dbname
      end)
      -- and one of those doesn't want to end with .com
      if @dbname <> 'net.Mathoverflow.meta' 
         set @dbname = 'com.' + @dbname
      exec #recursesiteurl @dbname,  @res OUTPUT
   end
GO
-- create url from dbname
IF OBJECT_ID ( '#recursesiteurl', 'P' ) IS NOT NULL 
    DROP PROCEDURE #recursesiteurl;
GO
create procedure #recursesiteurl
   @dbname nvarchar(250),
   @res nvarchar(250) OUT
   as
   begin
      declare @pos integer
      declare @rec nvarchar(250)
       
      set @res = @dbname
      set @pos = CHARINDEX('.', @dbname)
      if (@pos > 0 ) 
      begin
         set @rec = substring(@dbname, @pos+1, len(@dbname))
         exec #recursesiteurl @rec,  @rec output
         set @res = @rec 
                  + '.' 
                  + substring(@dbname, 0, @pos) 
      end
   end;
GO

-- all databases
declare  db_c cursor for select [name] 
                         from sys.databases 
                         where database_id > 5 -- skip master, temp, model, msdb, Data.SE
                         and ([name] not like '%.meta' or [Name] = 'Meta.StackExchange')

declare @db_c_name sysname   -- holds name of db after fetch
declare @sql nvarchar(max) -- holds build up sql string

-- result table
create table #all_results (site nvarchar(250)
                            , old bigint
                            , new bigint
                            , percentChange float);
                            
open db_c
fetch next from db_c into @db_c_name
while(@@FETCH_STATUS = 0)
begin
    set @sql = N'use '+ QUOTENAME(@db_c_name) +';
               declare @url nvarchar(250)
               exec #siteurl ''' + @db_c_name  + ''', @url output;
               with period1 as (select @url as siteUrl, count(*) as PostCount
                 from PostsWithDeleted
                 where CreationDate >= ''##start1##''
                 and CreationDate < ''##end1##''
                 and PostTypeId = 2
                 ),
               period2 as (select @url as siteUrl, count(*) as PostCount
                 from PostsWithDeleted
                 where CreationDate >= ''##start2##''
                 and CreationDate < ''##end2##''
                 and PostTypeId in (1, 2)
                 )
               insert into #all_results 
               select period1.siteUrl,
                      period1.PostCount, 
                      period2.PostCount,
                      round(100*(period2.PostCount/(period1.PostCount + 0.001) - 1), 1) 
               from period1 inner join period2 on period1.siteUrl = period2.siteUrl
               where period1.PostCount > 0
               ;'
    exec (@sql)
   fetch next from db_c into @db_c_name
end;
close db_c;
deallocate db_c;

select lower(
       replace(
       replace(
       replace(site,
         '.StackExchange.com',''),
         '.com',''),
         '.net','')) as [site]
     , old, new, percentChange
from #all_results
order by percentChange desc

drop table #all_results

drop procedure #recursesiteurl
drop procedure #siteurl

Enter Parameters

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