?
Solved

Group by month/Year in a SQL view

Posted on 2009-06-29
5
Medium Priority
?
1,149 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 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 1000 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

Python: Series & Data Frames With Pandas

Learn the basics of Python’s pandas library of series & data frames and how we can use these tools for data manipulation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

771 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