Untagged questions (without locks), networkwide

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

create table #results (site nvarchar(250), tagcount int);

declare @sql nvarchar(max) = ''

select @sql = @sql 
+ iif( len(@sql) > 1 
     , 'union'
     , 'insert into #results'
) +
N'
select ''' + name + ''', count(*)
  from ' + quotename(name) + '.dbo.tags as t
  inner join ' + quotename(name) + '.dbo.posttags as pt on pt.tagid = t.id
  inner join ' + quotename(name) + '.dbo.posts as p on p.id = pt.postid
  left outer join ' + quotename(name) + '.dbo.suggestededits as se on p.id = se.postid
    and se.approvaldate is null and se.rejectiondate is null
  outer apply (
    select top 1 posthistorytypeid from ' + quotename(name) + '.dbo.posthistory
      where postid = p.id and posthistorytypeid in (14, 15)
      order by creationdate desc
      ) oa(posthistorytypeid)
  where t.tagname = ''untagged''
    and se.id is null
    and (oa.posthistorytypeid is null or oa.posthistorytypeid = 15)
'
from sys.databases
where database_id > 5

exec (@sql);

-- Site details, cf. https://meta.stackexchange.com/a/359795/295232
CREATE TABLE #Sites (URL NVARCHAR(100), SiteName NVARCHAR(100), DatabaseName NVARCHAR(50));
-- fetch Markdown for post containing table with site details
DECLARE @md NVARCHAR(MAX);
SELECT TOP 1 @md = Text
  FROM [StackExchange.Meta].dbo.PostHistory
  WHERE PostId = 359796 AND PostHistoryTypeId = 5
  ORDER BY CreationDate DESC;
-- split Markdown on pipe symbol
CREATE TABLE #TableEntries (ID INT IDENTITY(1, 1), Value NVARCHAR(100));
INSERT INTO #TableEntries (Value)
SELECT LEFT(TRIM(VALUE), 100) FROM STRING_SPLIT(@md, '|')
  WHERE LEN(TRIM(REPLACE(REPLACE(VALUE, CHAR(10), ''), CHAR(13), ''))) > 0;
-- 'pivot' - we have to discard the introductory text, headers and horizontal lines
INSERT INTO #Sites
SELECT t0.Value, t1.Value, t2.Value
  FROM #TableEntries AS t0
  INNER JOIN #TableEntries AS t1
    ON t0.Id + 1 = t1.Id
  INNER JOIN #TableEntries AS t2
    ON t0.Id + 2 = t2.Id
  WHERE t0.Id >= 8 -- that's where the real content starts
    AND t0.Id % 3 = 2;

select URL + '/tags/untagged|' + sitename,
  tagcount
  FROM #results AS r
  INNER JOIN #Sites AS s
    ON r.Site = s.DatabaseName
  WHERE tagcount > 0
  ORDER BY tagcount DESC, sitename

drop table #results

Enter Parameters

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