Skip to main content
replaced http://data.stackexchange.com/ with https://data.stackexchange.com/
Source Link

In this multi database querymulti database query I'll fetch all PostNotices with PostNoticeTypeId 1 or 3. Those match with the two descriptions you've shown. For each PostId the Votes table is joined so we can sum the Up and Down votes. As we know the creationdate of the PostNotice and the creationdate of the vote (unfortunately at date precision) we can determine how many votes before the PostNotice and how many votes after the postnotice were cast.

Don't forget to give the tutorialtutorial a try.

In this multi database query I'll fetch all PostNotices with PostNoticeTypeId 1 or 3. Those match with the two descriptions you've shown. For each PostId the Votes table is joined so we can sum the Up and Down votes. As we know the creationdate of the PostNotice and the creationdate of the vote (unfortunately at date precision) we can determine how many votes before the PostNotice and how many votes after the postnotice were cast.

Don't forget to give the tutorial a try.

In this multi database query I'll fetch all PostNotices with PostNoticeTypeId 1 or 3. Those match with the two descriptions you've shown. For each PostId the Votes table is joined so we can sum the Up and Down votes. As we know the creationdate of the PostNotice and the creationdate of the vote (unfortunately at date precision) we can determine how many votes before the PostNotice and how many votes after the postnotice were cast.

Don't forget to give the tutorial a try.

Source Link
rene Mod
  • 92.1k
  • 17
  • 245
  • 516

Since PostNotices got added to the schema this has become a lot easier to answer.

In this multi database query I'll fetch all PostNotices with PostNoticeTypeId 1 or 3. Those match with the two descriptions you've shown. For each PostId the Votes table is joined so we can sum the Up and Down votes. As we know the creationdate of the PostNotice and the creationdate of the vote (unfortunately at date precision) we can determine how many votes before the PostNotice and how many votes after the postnotice were cast.

The whole query looks like this:

-- result table
create table #result(
     site sysname
   , postid int
   , upv_before int
   , upv_after int
   , dv_before int
   , dv_after int
)
GO
declare @sql nvarchar(max) -- holds sql union over all databases

-- build multi db union query
select @sql=concat(N'insert into #result ', string_agg(concat(N'
select ', quotename(name, ''''), N' site
     , pn.postid
     , sum(case 
           when v.votetypeid = 2 
            and v.creationdate < pn.creationdate  
           then 1 
           else 0
           end ) [upvotes before notice]
     , sum(case 
           when v.votetypeid = 2 
            and v.creationdate >= pn.creationdate  
           then 1 
           else 0
           end ) [upvotes after notice]
     , sum(case 
           when v.votetypeid = 3
            and v.creationdate < pn.creationdate  
           then 1 
           else 0
           end ) [downvotes before notice]
     , sum(case 
           when v.votetypeid = 3 
            and v.creationdate >= pn.creationdate  
           then 1 
           else 0
           end ) [downvotes after notice]

from ', quotename(name), '.dbo.postnotices pn
inner join ', convert(nvarchar(max),quotename(name)), '.dbo.postnoticetypes pnt on pnt.id = pn.postnoticetypeid
left outer join ', convert(nvarchar(max),quotename(name)), '.dbo.votes v on v.postid = pn.postid
where postnoticetypeid in (1,3)
and v.votetypeid in (2,3)
group by pn.postid
'),'union all'))
from sys.databases
where database_id > 5
and (name not like '%.Meta' or Name ='StackExchange.Meta')

exec (@sql)


-- build kind of useful stats based on what is in #result
select 0 grp
     , site [Site]
     , count(*) [total observations]
     , avg([upv_before]) [upvotes before notice]
     , avg([upv_after]) [upvotes after notice]
     , avg([dv_before]) [downvotes before notice]
     , avg([dv_after]) [downvotes after notice]
from #result
where site = db_name()
group by site
union
select 1
     , '[Total over all sites]' 
     , count(*) [total observations]
     , avg([upv_before]) [upvotes before notice]
     , avg([upv_after]) [upvotes after notice]
     , avg([dv_before]) [downvotes before notice]
     , avg([dv_after]) [downvotes after notice]
from #result
union
select 2
     , site
     , count(*) [total observations]
     , avg([upv_before]) [upvotes before notice]
     , avg([upv_after]) [upvotes after notice]
     , avg([dv_before]) [downvotes before notice]
     , avg([dv_after]) [downvotes after notice]
from #result
where site <> db_name()
group by site
order by 1 ,4 desc

When run today this is what the result will look like, with the Engineering site selected:

results of up and downvotes before and after a notice was added

Notice that you can switch SEDE to any site and re-run the query to get the results for any other site you're interested in. The query honors on which site you are.

I have removed all the Meta sites (but not Meta StackExchange) from the results because as we all know Meta is murder.

Keep in mind SEDE is only updated weekly, on Sunday morning.

Don't forget to give the tutorial a try.