I have table where I am having monthly data like below.
Here my AvgSpeedOfAnswer
column is calculated like this:
avg(SpeedOfAnswer)
Table:
Date | AvgSpeedOfAnswerMonth |
---|---|
7/1/2022 | 20.8 |
8/1/2022 | 22.6 |
9/1/2022 | 24.9 |
Now my requirement is I need to create a query where I can get quarterly data from above monthly table data.
I wrote a query like this:
SELECT
'Quarterly' AS TrendType,
DATEADD(Q, DATEDIFF(Q, 0, TrendStartdate), 0) AS TrendStartdate,
SUM(AvgSpeedOfAnswer)
FROM
Month_Stats
GROUP BY
DATEADD(Q, DATEDIFF(Q, 0, TrendStartdate), 0)
ORDER BY
DATEADD(Q, DATEDIFF(Q, 0, TrendStartdate), 0)
I am not sure what should I need to take for AvgSpeedOfAnswer
.
Is it SUM(AvgSpeedOfAnswerMonth)
or AVG(AvgSpeedOfAnswerMonth)
or AVG(SUM(AvgSpeedOfAnswerMonth))
?
Could anyone please suggest?
4条答案
按热度按时间8zzbczxx1#
As others mentioned. You need to use DATEPART
1dkrff032#
you can use DATEPART and QUARTER
fiddle
rlcwz9us3#
I think you need avg (AvgSpeedOfAnswerMonth) to get the quarterly average speed over the quarter. Sum(AvgSpeedOfAnswerMonth) and avg(sum(AvgSpeedOfAnswerMonth) give the same value, which is the sum of the quarterly values.
ogsagwnx4#
Use the DATEPART function with parameter QUARTER, like