Mark Wilson
asked on
SQL - Grouping by Quarters
Using SQL 2008
I am trying to develop a query that counts matters that were open at the end of each quarter.
The financial year runs from 1st May to 30th April of the next year
The quarters would be
Q1 – 1st May- 31st July
Q2 – 1st August – 31st October
Q3 - 1st November to 31st January
Q4 – 1st Feb to April 30th
There is data from the year 1996 onwards
At the moment the query is something like this
SELECT worktype , sum(case when opendate < '1996-08-01 00:00:00.000' and AND (closedate > '1996-08-01 00:00:00.000' OR closedate IS NULL) then 1 end) [Q1 -1996]
from Matter_T
group by worktype
There are case statements for each quarter to the present quarter, as you can imagine this is unwieldy and I have to keep adding new code when the next quarter comes along
Is there a better way of doing this?
I am trying to develop a query that counts matters that were open at the end of each quarter.
The financial year runs from 1st May to 30th April of the next year
The quarters would be
Q1 – 1st May- 31st July
Q2 – 1st August – 31st October
Q3 - 1st November to 31st January
Q4 – 1st Feb to April 30th
There is data from the year 1996 onwards
At the moment the query is something like this
SELECT worktype , sum(case when opendate < '1996-08-01 00:00:00.000' and AND (closedate > '1996-08-01 00:00:00.000' OR closedate IS NULL) then 1 end) [Q1 -1996]
from Matter_T
group by worktype
There are case statements for each quarter to the present quarter, as you can imagine this is unwieldy and I have to keep adding new code when the next quarter comes along
Is there a better way of doing this?
ASKER
Thanks for the answer
I don't think it will work in this case
I am counting the matters that were still open at the end of that quarter, which is different from the number opened in that quarter. I am using both the open and closed date to work it out, see previous code. So I cannot join to the dates table
I don't think it will work in this case
I am counting the matters that were still open at the end of that quarter, which is different from the number opened in that quarter. I am using both the open and closed date to work it out, see previous code. So I cannot join to the dates table
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
halifaxman - I just kicked out an article that deals specifically with Fiscal calendar planning --> SQL Server Calendar Table: Fiscal Years. Let me know if this helps you, and if yes please click on the 'Good Article' button and provide some feedback. Thanks.
ASKER
Jim - been ill so just got to this, thanks very much this is a very useful article
Open in new window
Now you can join in your matters table and group by fiscal year and fiscal quarter.