Legendary_Before_Grad

1

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for professional and enthusiast programmers

declare SITES cursor for
    select Substring(Name, 0, Len(Name) - 4)
    from sys.Databases
    where Name like '%.Meta'
     and not Name = 'StackExchange.Meta'
UNION
    select 'StackExchange.Meta'

create table #sitestats (
    Site NVarchar(250),
    Age Date,
    Election Date,
    Legendary Date);
    
declare @sitedbname as NVarchar(100),
        @query as NVarchar(1000)
    ;
    
open SITES

fetch next from SITES into @sitedbname
while @@FETCH_STATUS = 0
BEGIN

    declare @org as NVarchar(100), @http as NVarchar(100), @pos as int;

    -- convert the DBName in to a URL for the result set.
    select @org = lower(@sitedbname),
           @http = 'http://',
           @pos = CharIndex('.', @sitedbname);
    if @pos > 0 begin
        select @http = @http + substring(@org, @pos + 1, len(@org) - @pos) + '.',
               @org = left(@org, @pos - 1)
    end
    select @http = @http + @org + '.com'

    print 'Processing site ' + @sitedbname + ' -> ' + @http
    
    --Add whatever sub-URL you want to the http url:
    select @http = @http + '/users/'
    
    -- build and run your dynamic query. Note how you prefix the tables you need
    set @query = '
           insert into #sitestats
           select ''' + @http + '|' + @sitedbname + ''',
           Convert(Date, Min("Date")),
           Convert(Date, Min(case when Name = ''Caucus'' then "Date" else NULL end)),
           Convert(Date, Min(case when Name = ''Legendary'' then "Date" else NULL end))
           FROM [' + @sitedbname + ']..Badges'
          ; 
           
    EXEC sp_executesql @query

    fetch next from SITES into @sitedbname
END

close SITES
deallocate SITES
;

update #sitestats set Election = '2011-03-08' where Site like '%/unix.stack%';
update #sitestats set Election = '2011-03-01' where Site like '%/tex.stack%';
update #sitestats set Election = '2011-03-01' where Site like '%/english.stack%';
update #sitestats set Election = '2010-12-17' where Site like '%/math.stack%';
update #sitestats set Election = '2011-02-02' where Site like '%/stackoverflow%';
update #sitestats set Election = '2011-02-02' where Site like '%/superuser%';

with Results as (
  select Site,
       Age,
       Election,
       Legendary,
       Case when Legendary is not null
            and (Election is null OR Legendary < Election)
            then 'PRE'
            else ''
       end as "Advanced"
  from #sitestats
)
select *
from Results
order by Advanced Desc, Election desc, Age desc

Enter Parameters

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