Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 sql month question

Author  Topic 

vc56522
Starting Member

11 Posts

Posted - 2005-04-10 : 20:19:55
Hello, everyone
This is my problem: I want to be able to group by month and year
I have a table that look lik this:
date_created po_number
2004-02-01 001
2004-02-02 003
2005-02-05 123
2005-02-05 321
2005-03-06 322
2005-03-07 323
2005-03-08 324
I want the select statement to look like this:
Month Total_po_number
Feb(2004) 2
Feb(2005) 2
March 3

This is what I have try but it is only grouping by day.

SELECT DATENAME(mm,date_created) as Month, count(po_number) as Total_po_number
FROM PO_FILE
Group by DATENAME(mm,date_created),po_number
Order by date_created

Please help, What wrong with my code?

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2005-04-10 : 21:24:35
1) SELECT does not display the Year as defined in the specification.
2) GROUP BY is not grouping by Year in addition to Month as defined in the specification.
3) GROUP BY is grouping by po_number which is NOT in the specification. This is why it APPEARS to be grouping by day.
4) ORDER BY is using date_created instead of extracting the Month. Also, should include the Year portion.

HTH

=================================================================
In order to improve the mind, we ought less to learn than to contemplate.
-Rene Descartes, philosopher and mathematician (1596-1650)
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2005-04-10 : 23:20:12
It is rude to post your question on more than one forum;
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=48225

CODO ERGO SUM
Go to Top of Page
   

- Advertisement -