May 24, 2006 at 3:04 pm
Hi guys. I'm trying to determine a way to do this in one query.
I have a table with records spanning over a few months. I want to design a query that will calculate how many records (of certain conditions) exist for each month. There is a datetime field (REQ_DATE_COMPLETE) associated with each request. I explain best by example.. so here's a small one:
My table has records such as
1 Req 1 04/05/06
2 Req 2 04/09/06
3 Req 3 04/21/06
4 Req 4 05/04/06
5 Req 5 05/26/06
6 Req 6 07/12/06
I need a query that will return the following information:
MONTH Number of Requests Completed
Apr-06 3
May-06 2
Jun-06 0
Sep-06 1
Make sense? Am I expecting too much out of a single query? Would it be too convoluted? If anyone wants to take a crack at it or give me any hints I would appreciate it.
Thanks,
Sean
May 24, 2006 at 3:26 pm
This should get you started. You will have to add in all of the months and you will need to tweak it to handle the year.
create table monthgroup (RowID int identity(1,1), MonthDate datetime)
insert into monthgroup
select '04/05/06' union all
select '04/09/06' union all
select '04/21/06' union all
select '05/04/06' union all
select '05/26/06' union all
select '07/12/06'
select CASE datepart(m,monthdate)
when 4 then 'April'
when 5 then 'May'
when 7 then 'July'
end as 'Month',
count(*) as 'Number of Requests Completed'
from MonthGroup
group by datepart(m,monthdate)
May 24, 2006 at 6:19 pm
, count(*) [Number of Requests Completed]
from MonthGroup
group by datename(m,datepart(m,monthdate))
Tim Wilkinson
"If it doesn't work in practice, you're using the wrong theory"
- Immanuel Kant
May 25, 2006 at 6:52 am
Note that, in both of the above examples, April 2005 and April 2006 will be counted as the same thing. It's best to also add a DATEPART(y,monthdate) to your GROUP BY and SELECT clauses.
As an additional note, most reporting applications (Access, Crystal, etc.) will do this much more easily. Depending on your needs, that might not help. But, it is nice to remember that you don't have to do all the work in TSQL.
May 26, 2006 at 12:27 am
Aw, what the heck... do it all in SQL... pick your poison...
SELECT STUFF(RIGHT(CONVERT(CHAR(11),MonthDate,113),8),4,3,'-') AS [Month],
COUNT(*) AS [Number of Requests Completed]
FROM #MonthGroup
GROUP BY STUFF(RIGHT(CONVERT(CHAR(11),MonthDate,113),8),4,3,'-')
SELECT LEFT(DATENAME(mm,MonthDate),3)+'-'+RIGHT(DATENAME(yy,Monthdate),2) AS [Month],
COUNT(*) AS [Number of Requests Completed]
FROM #MonthGroup
GROUP BY LEFT(DATENAME(mm,MonthDate),3)+'-'+RIGHT(DATENAME(yy,Monthdate),2)
--Jeff Moden
Change is inevitable... Change for the better is not.
May 26, 2006 at 5:17 am
Check This Out:-
select month(REQ_DATE_COMPLETE) Req_Month ,year(REQ_DATE_COMPLETE) Req_Year,count(REQNO)
from REQ_DATE
group by month(REQ_DATE_COMPLETE) ,year(REQ_DATE_COMPLETE)
order by year(REQ_DATE_COMPLETE) , month(REQ_DATE_COMPLETE)
This way you will have more control and more options to represent your data in different ways.
May 26, 2006 at 7:01 am
Requirement is to show missing months as well
DECLARE @from datetime, @diff int
SELECT @from = DATEADD(month,DATEDIFF(month,0,MIN([Date])),0),
@diff = DATEDIFF(month,MIN([Date]),MAX([Date]))
FROM @Table
SELECT STUFF(CAST(d.[Date] as char(11)),4,6,'-'), COUNT(a.[Date])
FROM (SELECT DATEADD(month,n.number,@from) AS [Date]
FROM master.dbo.spt_values n
WHERE n.type = 'P' AND n.number BETWEEN 0 AND @diff) d
LEFT OUTER JOIN @table a ON DATEADD(month,DATEDIFF(month,0,a.[Date]),0) = d.[Date]
GROUP BY d.[Date]
ORDER BY d.[Date]
I used undocumented table spt_values to get numbers to test with but you should create your own Numbers table
Far away is close at hand in the images of elsewhere.
Anon.
May 26, 2006 at 8:58 pm
Requirement is to show missing months as well |
Dang... missed that one, David... nice catch.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 30, 2006 at 2:59 am
Dang... missed that one, David... nice catch. |
Thanks Jeff
But it is you normally catching my misses...
Far away is close at hand in the images of elsewhere.
Anon.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply