Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Group By Month SQL Query

Posted on 2011-03-11
4
Medium Priority
?
598 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:
Ephraim Wangoya earned 1336 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 664 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:Ephraim Wangoya
Ephraim Wangoya earned 1336 total points
ID: 35112526

@Cboudroz
Quite right, oversite on my part
0
 

Author Closing Comment

by:jazzcatone
ID: 35112689
Thanks
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

782 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