Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Steve2106
Posting Yak Master
183 Posts |
Posted - 2009-12-12 : 09:56:10
|
| Hi There,Can you help me please.I have a table of events and I need to display a count of events for each month of each year. The table layout looks like this:AutoIdEventDateSample Data:1, Piano Concert, 01/09/20092, Members Gala, 02/09/20093, Residents Arobic class, 15/10/20094, Races Night, 12/01/2010I need to create a query count which would show:year: Month (Count)2009: Sept (2) | Oct (1) 2010: Jan (1)Hope that makes sense and someone can help.Best Regards,Steve |
|
|
a.rameshk
Starting Member
19 Posts |
Posted - 2009-12-12 : 10:52:41
|
| Hi,The below query can helpSELECT DATEPART(YY,DATES) as [Year],DATENAME(MM,Dates)as [Month],Count(Event)as [Count]FROM GetEventGROUP BY DATEPART(YY,DATES),DATENAME(MM,Dates) |
 |
|
|
Steve2106
Posting Yak Master
183 Posts |
Posted - 2009-12-14 : 03:50:35
|
| Hi There,Thanks for your help I appreciate you taking the time.Best Regards, |
 |
|
|
Steve2106
Posting Yak Master
183 Posts |
Posted - 2009-12-14 : 09:22:17
|
| Hi there,This worked great but I do have a problem.It displays in alphabetical month order so April comes before Jan & feb. Is there any way that I can show. Jan 2009 10Feb 2009 04Mar 2009 11Apr 2009 03Etc.Thanks for your help I appreciate it.Best Regards,Steve |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-12-14 : 09:24:37
|
Include ORDER BY DATENAME(MM,Dates) in the end of the query |
 |
|
|
Steve2106
Posting Yak Master
183 Posts |
Posted - 2009-12-14 : 10:11:44
|
| Hi There,I have managed to get the table sorted by the month number using "MONTH(DateRaised)"The last thing I need now is to have an acumalative total.Mon | Year |Count | AccJan | 2009 | 10 | 10Feb | 2009 | 04 | 14Mar | 2009 | 11 | 25Apr | 2009 | 03 | 28I hope that is doable.Thanks for your help once again.Best Regards,Steve |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-12-14 : 10:22:39
|
| Accumulative total of what? its not there in your sample data or your expected output? |
 |
|
|
Steve2106
Posting Yak Master
183 Posts |
Posted - 2009-12-14 : 10:44:19
|
quote: Originally posted by vijayisonly Accumulative total of what? its not there in your sample data or your expected output?
Sorry,Acumalative total of Count(Event). I have shown it in my post above. (acc)I have used the query that a.rameshk gave as that worked perfectly (2nd Post).Thanks for taking the time to help.Best Regards,Steve |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-12-14 : 11:48:23
|
Here's an example on how to do it with your sample data..Might not be really fast..but it'll workdeclare @t table (id int,mon varchar(20), yr int,cnt int)insert @tselect 1,'Jan', 2009, 10 union allselect 2,'Feb', 2009, 04 union allselect 3,'Mar', 2009, 11 union allselect 4,'Apr', 2009, 03 select *, (select sum(cnt) from @t t2 where t2.id <= t1.id) as accfrom @t t1 |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-12-14 : 12:11:34
|
Here's one from your initial sample data from your first post...Sample datadeclare @t table (Autoid int, [Event] varchar(100), Date datetime)insert @tselect 1, 'Piano Concert', '09/01/2009' union allselect 2, 'Members Gala', '09/02/2009' union allselect 3, 'Residents Arobic class', '10/15/2009' union allselect 4, 'Races Night', '01/12/2010' Query;with Seqtable(id,[Year],[Month],[Count])AS ( SELECT row_number() over(order by mth) as id,[Year],[Month],[Count] from ( select DATEPART(YY,Date) as [Year],DATENAME(MM,Date)as [Month],datepart(mm,Date) as mth,Count(Event)as [Count] FROM @t GROUP BY DATEPART(YY,Date),DATENAME(MM,Date),datepart(mm,Date) ) t )select *, (select sum([Count]) from Seqtable t2 where t2.id <= t1.id) as accfrom Seqtable t1 |
 |
|
|
Steve2106
Posting Yak Master
183 Posts |
Posted - 2009-12-15 : 06:16:25
|
| Hi vijayisonly,Thanks for the replies.I'll try to incorporate your examples.I appreciate your help.Best regards,Steve |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-12-15 : 09:57:32
|
| Np...You're welcome |
 |
|
|
|
|
|
|
|