Q&A about the site for researchers and developers who explore the principles of a system through analysis of its structure, function, and operation
---based on this template: https://data.stackexchange.com/stackoverflow/query/1004129/multi-db-template-with-hostname ---I count posts with multiple duplicate targets multiple tims ---since I am using this: SUM(Case When (pl.LinkTypeId=3) Then 1 Else 0 End) ---However, I do not know an easy way to correct this. declare @sql nvarchar(max) create table #result (site sysname , hostname sysname , countdeldupl integer , countdelall integer , percentage decimal(5,2) ) select @sql = N'insert into #result' + STRING_AGG(concat(N' select ''', name ,N''' as site , ''',hostname ,N''' , count(distinct Case When (pl.LinkTypeId=3) Then pl.PostId Else null End) , COUNT(DISTINCT p.Id) , ROUND((CAST((count(distinct Case When (pl.LinkTypeId=3) Then pl.Postid Else null End)) AS decimal))*100.0/(CAST((COUNT(DISTINCT p.Id)) AS decimal)),2) from ', quotename(name), N'.dbo.postlinks pl right outer join ', quotename(name), N'.dbo.postswithdeleted p on pl.PostId=p.Id where (p.PostTypeId=1) AND (p.DeletionDate IS NOT NULL) '),N' union all') from (select convert(nvarchar(max),name) name -- , convert(nvarchar(max),sitename) sitename -- , meta -- , domain , concat( -- based on an idea from Glorfindel -- https://meta.stackexchange.com/a/321181 (case sitename WHEN 'Audio' THEN 'video' WHEN 'Beer' THEN 'alcohol' WHEN 'CogSci' THEN 'psychology' WHEN 'Garage' THEN 'mechanics' WHEN 'Health' THEN 'medicalsciences' WHEN 'Moderators' THEN 'communitybuilding' WHEN 'Photography' THEN 'photo' WHEN 'Programmers' THEN 'softwareengineering' WHEN 'Vegetarian' THEN 'vegetarianism' WHEN 'Writers' THEN 'writing' WHEN 'Br' THEN 'pt' WHEN 'Mathoverflow' THEN concat((meta+'.'), sitename) ELSE case when sitename = domain then null else sitename end end +'.') , (case when sitename <> 'mathoverflow' then (meta+'.') else null end) , (case when sitename <> 'mathoverflow' then concat((domain + '.'), 'com') else 'net' end) ) hostname from ( select name , case parsename(name,1) when 'Meta' then parsename(name,2) else parsename(name,1) end [sitename] , case parsename(name,1) when 'Meta' then 'meta' else null end [meta] , coalesce(parsename(name,3), parsename(name,2)) [domain] from sys.databases where database_id > 5 and (name not like '%.Meta' or name = 'StackExchange.Meta') ) dbs ) dbsall exec (@sql) select * from #result order by percentage DESC