Is there a good Data Explorer query to find specific user's longest answers?

0

Please login or register to vote for this query.

(click on this box to dismiss)

http://meta.stackexchange.com/questions/280235/is-there-a-good-data-explorer-query-to-find-specific-users-longest-answers/280236?noredirect=1#comment909748_280236

Meta Stack Overflow

Q&A about the site for professional and enthusiast programmers

---- accountid: Account on stackexchange.com! "In the url on stackexchange.com"
-- 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

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

declare @p_accountid int = ##accountid:int?6085540## -- user

-- result table
create table #all_posts ( site nvarchar(250)
                            , id int
                            , title nvarchar(250)
                            , length int
                            , owneruserid int
                            , displayname nvarchar(200));
                            
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
               insert into #all_posts 
               select top 1 @url
               , p.id
               , case when posttypeid=2 then
                   (select title from posts where id = p.parentid)
                 else p.title
                 end
               , len(p.body)
               , p.owneruserid
               , case when u.id is null 
                 then p.ownerdisplayname
                 else u.displayname
                 end
               from posts p
               left outer join users u on u.id = p.owneruserid 
               -- postttypeid in (1,2)
               where posttypeid = 2 -- answers 
               -- remove next line to get longest posts per site
               and u.accountid = ' + cast(@p_accountid as nvarchar) + '
               order by len(p.body) desc;'
    exec (@sql)
   fetch next from db_c into @db_c_name
end;
close db_c;
deallocate db_c;

select replace(
       replace(
       replace(site,
         '.StackExchange.com',''),
         '.com',''),
         '.net','') as [site]
     , 'http://' 
     + site 
     + '/q/'
     + cast(id as nvarchar)
     + '|'
     + title as question
     , length
     , 'http://' 
     + site 
     + '/user/'
     + cast(coalesce(owneruserid, -1) as nvarchar)
     + '|'
     + displayname as [Owner]
from #all_posts
order by length desc

drop table #all_posts

drop procedure #recursesiteurl
drop procedure #siteurl

Enter Parameters

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