I apologize if this is the incorrect location for this - please move it if so! I've been trying for the past several hours to come up with a query that will return the following information:
The number of Questions and number of Answers per "Top 5" Tag from the last three completed months (April 2012, May 2012, June 2012).
I've gotten this far, based on other queries I've found:
SELECT *, COUNT(*) AS Count FROM
(
SELECT CAST(
CAST(DATEPART(YYYY, CreationDate) AS varchar) + '-' + CAST(DATEPART(MM, CreationDate) AS varchar) + '-01'
AS DATETIME) [Month],
t.TagName
FROM Posts p
JOIN PostTags pt ON pt.PostId = p.Id
JOIN Tags t ON t.Id = pt.TagId
WHERE TagId IN
(
SELECT TOP 5 Id FROM Tags
ORDER BY Count DESC
)
) AS X
WHERE [Month] >= CAST('2012-04-01' as datetime)
AND [Month] < CAST('2012-07-01' as datetime)
GROUP BY TagName, [Month]
ORDER BY [Month] ASC, TagName ASC
The issue is that the JOIN eliminates records that don't have associated PostTags. I am not extremely skilled with SQL, so this has caused me some problems. Relevent Information:
- Questions are Posts with PostTypeId set to 1
- Answers are Posts with PostTypeId set to 2
If anyone can help I would be extremely appreciative.
Thanks.