search
Japanese Chinese Nederlands Espanol Italiano Deutsch Francais Twitter Rss Feeds
SQL Server GroupsView
SQL Server Ce
SQL Server Clients
SQL Server Clustering
SQL Server Connect
SQL Server Dts
SQL Server Fulltext
SQL Server Integrationsvcs
SQL Server Msde
SQL Server Newusers
SQL Server Olap
SQL Server Programming
SQL Server Replication
SQL Server Reportingsvcs
SQL Server Security
SQL Server Server
SQL Server Setup
SQL Server Tools
SQL Server Xml

Group SummariesView
.NET Framework
Access
BizTalk
Certifications
CRM
DDK
Exchange Server
FoxPro
French
French .NET
Games
German
German .NET
Graphic Design
IIS
Internet
ISA Server
Italian
Italian .NET
Maps
MCIS
Miscellaneous
Mobile Application Development
Money
MSN
Networking
Office
Ops Mgr
Publisher
Security
SharePoint
Small Business
Spanish
Spanish .NET
SQL Server
Systems Management Server
Transaction Server
Virtual PC / Virtual Server
Visual Studio
Win32
Windows 2000
Windows 2003 Server
Windows 7
Windows Live
Windows Media
Windows Update
Windows Vista
Windows XP
 

View All Microsoft SQL Server Programming Posts  Ask A New Question 

SQL syntax (Newbie Question) - Alastair MacFarlane

Tuesday, August 19, 2008 10:55 AM

Dear Group,

I have a table (Members) which has one field (for arguement's sake) which is
a datetime field called JoinDate which stores various dates.

I would like to create a SQL statement that could give me the aggregated
result:

August : 3
July : 45
June: 84

It is a total for each month. I would assume that I would use the DatePart
function to group and then count and specify that I only want the last three
months data but I am unsure of how to do this exactly.

Any hints would be appreciated. Thanks again.

Alastair MacFarlane
reply
 

ArunDhaJThanks for the speedy reply. - Alastair MacFarlane

Tuesday, August 19, 2008 11:29 AM

ArunDhaJ

Thanks for the speedy reply.

How could I specify for the last three months including the month we are
currently in? I presume this would be the WHERE clause.

Thanks again.

Alastair
reply

Select DatePart(mm, JoinDate), Count(DatePart(mm, JoinDate)) FromMembers Group - ArunDhaJ

Thursday, August 21, 2008 11:08 PM

Select DatePart(mm, JoinDate), Count(DatePart(mm, JoinDate)) From
Members Group By DatePart(mm, JoinDate)

Hope I understood the problem correct. This query may help...


-ArunDhaJ
reply

SQL syntax (Newbie Question) - Plame

Thursday, August 21, 2008 11:08 PM

You can use the DATENAME function if you need to get results as
posted:

SELECT DATENAME(MONTH, JoinDate) AS join_month,
COUNT(*) AS cnt
FROM Members
GROUP BY DATENAME(MONTH, JoinDate);

However, this may not give you the correct results if the data spans
over multiple years. You can include the year in the grouping:

SELECT CONVERT(CHAR(7), JoinDate, 126) AS join_year_month,
COUNT(*) AS cnt
FROM Members
GROUP BY CONVERT(CHAR(7), JoinDate, 126);


Plamen Ratchev
http://www.SQLStudio.com
reply

SQL syntax (Newbie Question) - Plame

Thursday, August 21, 2008 11:08 PM

I forgot to include the conditions for the last 3 months:

SELECT DATENAME(MONTH, JoinDate) AS join_month,
COUNT(*) AS cnt
FROM Members
WHERE JoinDate >= DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP)
- 2,
0)
AND JoinDate < DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP)
+ 1,
0)
GROUP BY DATENAME(MONTH, JoinDate);

SELECT CONVERT(CHAR(7), JoinDate, 126) AS join_year_month,
COUNT(*) AS cnt
FROM Members
WHERE JoinDate >= DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP)
- 2,
0)
AND JoinDate < DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP)
+ 1,
0)
GROUP BY CONVERT(CHAR(7), JoinDate, 126);


Plamen Ratchev
http://www.SQLStudio.com
reply

Previous Microsoft SQL Server Programming conversation.