Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on How to easily support time frame grouping in queries?

Post

How to easily support time frame grouping in queries?

+3
−0

I had a curiosity about how much the experienced users wait for their questions to be answered on Stack Overflow and had written a query for it:

SELECT YEAR(q.CreationDate) * 100 + MONTH(q.CreationDate) AS YM, COUNT(1) Cnt 
INTO #a_cte
FROM Posts AS q
   INNER JOIN Users qu ON qu.Id = q.OwnerUserId and qu.Reputation >= 1000
   WHERE q.CreationDate BETWEEN '20190101' AND '20210301'
   AND EXISTS (
     SELECT 1 
     FROM Posts a 
     where a.PostTypeId = 2 and a.ParentId = q.Id and a.Score > 1
     and datediff(day, q.CreationDate, a.CreationDate) <= 7
  )
  AND q.PostTypeId = 1
  GROUP BY YEAR(q.CreationDate) * 100 + MONTH(q.CreationDate);
 
SELECT YEAR(q.CreationDate) * 100 + MONTH(q.CreationDate) AS YM, COUNT(1) Cnt
INTO #q_cte
FROM Posts AS q
 INNER JOIN Users qu ON qu.Id = q.OwnerUserId and qu.Reputation >= 1000
WHERE q.CreationDate BETWEEN '20190101' AND '20210301'
 AND q.PostTypeId = 1
GROUP BY YEAR(q.CreationDate) * 100 + MONTH(q.CreationDate);
 
SELECT CAST(a.YM / 100 AS VARCHAR) + '-' + CAST(a.YM % 100 AS VARCHAR) + '-01', a.Cnt * 100.0 / q.Cnt AS AnsweredRatio
FROM #a_cte a
  JOIN #q_cte q ON q.YM = a.YM
ORDER BY a.YM

The performance is rather poor and I think it is also related to the fact that I have to compute the year-month and also group by it. This should have been much easier if the data model supplied some "dimension" columns such as year, month, year-month.

I know that enterprise solutions involve reporting databases and cubes, but I am wondering about simpler solutions (less maintenance for medium-sized data volumes). Is adding of computed columns a good way to support the queries I want. Examples:

  • CreationDateYear = YEAR(CreationDate)
  • YM = YEAR(q.CreationDate) * 100 + MONTH(q.CreationDate) AS YM

Or maybe a better approach is to have a job that computes similar columns in separate tables (as opposed to the real-time nature of the computed columns).

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (2 comments)
General comments

Skipping 2 deleted comments.