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 |
 |
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 |
 |
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 |
 |
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 |
 |