Solved

Group By Month SQL Query

Posted on 2011-03-11
4
586 Views
Last Modified: 2012-05-11
I have the below query which shows totals for the month. Question is how do I show the month in a fourth column with the actual month. (Saying like "January" for example). Any help would be greatly appreciated.


SELECT     COUNT(studyTbl.studyid_i) as study_count, companyTbl.companyname_vc
FROM         physicianTbl INNER JOIN
                      companyTbl ON physicianTbl.companyid_i = companyTbl.companyid_i INNER JOIN
                      studyTbl ON physicianTbl.physicianid_i = studyTbl.primarycarephysicianid_i
 GROUP BY Month(studydt_dt) , companyTbl.companyname_vc

Open in new window

0
Comment
Question by:jazzcatone
  • 2
4 Comments
 
LVL 32

Accepted Solution

by:
ewangoya earned 334 total points
ID: 35112429
try
SELECT     COUNT(studyTbl.studyid_i) as study_count, companyTbl.companyname_vc, DATENAME(MONTH, studydt_dt) [Month]
FROM         physicianTbl INNER JOIN
                      companyTbl ON physicianTbl.companyid_i = companyTbl.companyid_i INNER JOIN
                      studyTbl ON physicianTbl.physicianid_i = studyTbl.primarycarephysicianid_i
 GROUP BY Month(studydt_dt) , companyTbl.companyname_vc

Open in new window

0
 
LVL 7

Assisted Solution

by:Cboudroz
Cboudroz earned 166 total points
ID: 35112501
ewangoya:

add a MIN() on the studydt_dt if you want to make it work.

or group on DATENAME(MONTH, studydt_dt) instead of grouping on Month(studydt_dt)


 
SELECT     COUNT(studyTbl.studyid_i) as study_count, companyTbl.companyname_vc, DATENAME(MONTH, studydt_dt) [Month]
FROM         physicianTbl INNER JOIN
                      companyTbl ON physicianTbl.companyid_i = companyTbl.companyid_i INNER JOIN
                      studyTbl ON physicianTbl.physicianid_i = studyTbl.primarycarephysicianid_i
 GROUP BY DATENAME(MONTH, studydt_dt) , companyTbl.companyname_vc

Open in new window



also make sure that you understand that if you have 2 year of data in you table, you will receive the count of January 2010 and January 2011 on the same row, normally you should group by "year and month".



0
 
LVL 32

Assisted Solution

by:ewangoya
ewangoya earned 334 total points
ID: 35112526

@Cboudroz
Quite right, oversite on my part
0
 

Author Closing Comment

by:jazzcatone
ID: 35112689
Thanks
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Upgrading my SSIS package in VS 2012 6 59
Acces SQL Insert Problem 6 31
SQL Quer 4 21
MS SQL query to show nearest date 6 35
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

856 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