Solved

Group By Month SQL Query

Posted on 2011-03-11
4
584 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

920 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

18 Experts available now in Live!

Get 1:1 Help Now