tags:

views:

64

answers:

1

Inspired by this question on Meta, I wrote two queries on the Stack Exchange Data Explorer, one that counts the total number of Questions Asked by Month on SO and another that counts the Bounties Awarded by Month. How can I combine them so that I have the output in one query? I'd like to see Year, Month, Questions, Bounties, and Amount in one report.

Questions are recorded in the Posts table where PostTypeId = 1, but bounties are recorded in the Votes table where VoteTypeId = 9.

+4  A: 

select datepart(year, Posts.CreationDate) Year, datepart(month, Posts.CreationDate) Month, count(Posts.Id) Questions, count(Votes.Id) Bounties, sum(Votes.BountyAmount) Amount from Posts left join votes on datepart(year, Posts.CreationDate) = datepart(year, Votes.CreationDate) and datepart(month, Posts.CreationDate) = datepart(month, Votes.CreationDate) where PostTypeid = 1 -- 1 = Question and Votes.VoteTypeId = 9 -- 9 = BountyAwarded group by datepart(year, Posts.CreationDate), datepart(month, Posts.CreationDate) order by datepart(year, Posts.CreationDate), datepart(month, Posts.CreationDate); Does this work?

Please be kind. I wrote this in notepad & haven't worked with data explorer on SO.

EDIT:

select Isnull(V.Year, P.Year) As Year,
Isnull(V.Month, P.Month) As Month,
Isnull(V.Bounties, 0) As Bounties,
Isnull(V.Amount,0) As Amount ,
P.Questions
FROM
(
select
datepart(year, Posts.CreationDate) Year,
datepart(month, Posts.CreationDate) Month,
count(Posts.Id) Questions
from Posts
where PostTypeid = 1 -- 1 = Question
group by datepart(year, Posts.CreationDate), datepart(month, Posts.CreationDate)
) AS P
left JOIN
(
select
datepart(year, Votes.CreationDate) Year,
datepart(month, Votes.CreationDate) Month,
count(Votes.Id) Bounties,
sum(Votes.BountyAmount) Amount
from Votes
where VoteTypeId = 9 -- 9 = BountyAwarded
group by datepart(year, Votes.CreationDate), datepart(month, Votes.CreationDate)
) AS V
ON P.Year = V.Year AND P.Month = V.Month
order by P.Year, P.Month
shahkalpesh
This times out when I try to run it in SEDE. I tried almost *exactly* the same thing before posting this question. The only difference was that I grouped and ordered by Votes.CreationDate. At least I know my initial attempt wasn't completely silly if someone else came up with the same thing. :)
Bill the Lizard
Updated. Please check.
shahkalpesh
Looks like you nailed it on the second attempt though. Thank you! Do you want to save this on SEDE under your name, or can I just edit the original?
Bill the Lizard
@Bill: Feel free to add it to SEDE :)
shahkalpesh
Thanks a lot. Anyone who's interested in the stats can run the query [Bounties and Questions by Month](http://odata.stackexchange.com/stackoverflow/s/358/bounties-and-questions-by-month).
Bill the Lizard