Solved

Group by month/Year in a SQL view

Posted on 2009-06-29
5
1,142 Views
Last Modified: 2012-05-07
Hello all

I have a SOL table with dates in. I need to group by month and year and get a count all within a view if possible. Any ideas please

Many thanks
0
Comment
Question by:bostonste
5 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 24735152
this should be a good starter:
select convert(varchar(7), your_date_field, 120) yyy_mn , count(*), sum(somefield)
  from yourtable
group by convert(varchar(7), your_date_field, 120) 

Open in new window

0
 
LVL 22

Assisted Solution

by:8080_Diver
8080_Diver earned 250 total points
ID: 24735334
Alternatively, if you want your month and year separately displayed, you could try:
SELECT MyYear, 

       MyMonth,

       COUNT(*) AS MonthYearCount

FROM

(

SELECT CONVERT(VARCHAR(4), your_date_field, 120) AS MyYear,

       CONVERT(VARCHAR(2), your_date_field, 110) AS MyMonth 

FROM   yourtable

) Z

GROUP BY MyYear, 

         MyMonth;

Open in new window

0
 

Author Closing Comment

by:bostonste
ID: 31597868
Thanks chaps, got what i needed from both of you
0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24735352
I usually use this formula to get the vaue in YYYYMM format because it is easier when you sort. Sorts by first Year and then by Month:
SELECT  Year(Date)*100+Month(Date) as YearMth, SUM(Columns) Column

From    TableName

Group by Year(Date)*100+Month(Date)

Order by Year(Date)*100+Month(Date)

Open in new window

0
 
LVL 1

Expert Comment

by:angelsergio_e
ID: 24747506

How about this:

SELECT
       CLIENT=MYCLIENTNAME,
       HOWMANY=COUNT(*),
       COST=SUM(MYCOST),
       nMonth=DATEPART(month,MYDATE),
       nYEAR=DATEPART(year,MYDATE)

FROM
        MYTABLE

GROUP BY MYCLIENTNAME, DATEPART(month,MYDATE), DATEPART(year,MYDATE)

ORDER BY MYCLIENTNAME, DATEPART(month,MYDATE), DATEPART(year,MYDATE)
Hope this help!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now