[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Using Group by on mm/yyyy

Posted on 2009-02-19
13
Medium Priority
?
684 Views
Last Modified: 2012-05-06
I am trying to use group by on just mm/yyyy.

I wish to display the data in a chart as follows:

mm/yyyy on the x axis and the count of the jobs for that period on the y axis.

Any help with writing the query is appreciated :-)
0
Comment
Question by:IUAATech
  • 5
  • 3
  • 2
  • +3
13 Comments
 
LVL 11

Expert Comment

by:Otana
ID: 23682332
Try this:
SELECT MONTH(DateField), YEAR(DateField), count(Jobs) FROM Table1 GROUP BY MONTH(DateField), YEAR(DateField) ORDER BY YEAR(DateField), MONTH(DateField)
0
 
LVL 60

Assisted Solution

by:chapmandew
chapmandew earned 400 total points
ID: 23682347
you could do something like this:

select cast(datepart(m, datefield) as varchar(5))+ '/' + cast(datepart(yyyy, datefield) as varchar(5)), job, count(*)
from tablename
group by cast(datepart(m, datefield) as varchar(5))+ '/' + cast(datepart(yyyy, datefield) as varchar(5)), job

group by
0
 

Expert Comment

by:SwamyN
ID: 23682349
the information you have provided is quite vague. Queries retrieve data from tables. It would be helpful if you could provide sample data in the tables and the kind of output you are expecting.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:sm394
ID: 23682375

select right( convert(nvarchar, SessionCreationDate,103),7), count(*)
from Session
group  by  right(convert(nvarchar, SessionCreationDate,103),7)
0
 

Author Comment

by:IUAATech
ID: 23682457
Thanks guys.

chapmandew's solution works for me.

However, I also need to do a "order by" on the date.

Since the query is returning a varchar, "order by" sorts based on varchar, not datetime.

How can I fix this?
0
 

Author Comment

by:IUAATech
ID: 23682462
here is a sample of what I get when I use ORDER BY:

1/2006      81
1/2007      145
1/2008      79
10/2005      16
10/2006      146
10/2007      140
10/2008      63
11/2005      77
11/2006      134
0
 
LVL 25

Expert Comment

by:reb73
ID: 23682548
Change the select statement in chapmandew's solution as follows -

select right('0' + cast(datepart(m, datefield) as varchar(5)), 2) + '/' + cast(datepart(yyyy, datefield) as varchar(5)), job,


This prefixes a 0 to single digit months which will order correctly..

0
 

Author Comment

by:IUAATech
ID: 23682599
That didn't work.

here is my query:
SELECT     RIGHT('0' + CAST(DATEPART(m, CreatedOn) AS varchar(5)), 2) + '/' + CAST(DATEPART(yyyy, CreatedOn) AS varchar(5)) AS DatePeriod, COUNT(*) AS TotalJobs
FROM         dbo.ComputerSupportRequest
GROUP BY RIGHT('0' + CAST(DATEPART(m, CreatedOn) AS varchar(5)), 2) + '/' + CAST(DATEPART(yyyy, CreatedOn) AS varchar(5))
ORDER BY dateperiod

and I got the following:
01/2006      81
01/2007      145
01/2008      79
02/2006      91
02/2007      133
02/2008      125
03/2005      5
03/2006      67
03/2007      81
03/2008      79
04/2005      9
04/2006      66
04/2007      85
04/2008      82
05/2005      17
05/2006      73
05/2007      98
05/2008      167
0
 
LVL 11

Expert Comment

by:Otana
ID: 23682636
ORDER BY YEAR(CreatedOn), MONTH(CreatedOn)
0
 

Author Comment

by:IUAATech
ID: 23682675
If I am going to use "CreatedOn" in ORDER BY, then it has to exist in GROUP BY also.

So, my query now looks like
SELECT     RIGHT('0' + CAST(DATEPART(m, CreatedOn) AS varchar(5)), 2) + '/' + CAST(DATEPART(yyyy, CreatedOn) AS varchar(5)) AS DatePeriod, COUNT(*) AS TotalJobs
FROM         dbo.ComputerSupportRequest
GROUP BY RIGHT('0' + CAST(DATEPART(m, CreatedOn) AS varchar(5)), 2) + '/' + CAST(DATEPART(yyyy, CreatedOn) AS varchar(5)), CreatedOn
ORDER BY YEAR(CreatedOn), MONTH(CreatedOn)

and here is what I get:
03/2005      1
03/2005      1
03/2005      1
03/2005      1
03/2005      1
04/2005      1
04/2005      1
04/2005      1
04/2005      1
04/2005      1
04/2005      1
04/2005      1
04/2005      1
0
 
LVL 13

Expert Comment

by:sm394
ID: 23682734
select
d.dt,
d.cnt,
convert(datetime, '1/'+d.dt,103) as dd
from
(
      select  right ( convert(nvarchar, SessionCreationDate,103),7) as dt, count(*) as cnt
      from Session
      group  by   right(convert(nvarchar, SessionCreationDate,103),7)
) d
order by dd asc

0
 

Author Comment

by:IUAATech
ID: 23682825
sm394,
your solution works. Thanks.

However, is there a way to not return 'dd' in the result set?
0
 
LVL 13

Accepted Solution

by:
sm394 earned 1600 total points
ID: 23683008
select t1.dt,t1.cnt
from(
      select
      d.dt,
      d.cnt,
      convert(datetime, '1/'+d.dt,103) as dd
      from
      (
            select  right ( convert(nvarchar, SessionCreationDate,103),7) as dt, count(*) as cnt
            from Session
            group  by   right(convert(nvarchar, SessionCreationDate,103),7)
      ) d)t1
order by t1.dd asc
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

830 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