Solved

Group By Month SQL Query

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL server 2008 SP4 29 33
Sort by Month and Year - SQL 3 22
How to query date ranges with SQL 6 23
t-sql splitting name column 5 22
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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.

708 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

11 Experts available now in Live!

Get 1:1 Help Now