|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Count Customers FOr each monthHello,
I have a table with 3 columns ProductID int, Product varchar(30), OrderDate datetime I have orders in this table from January to July I want to get a query that will display how many orders I am getting per month and per week. Lontae Jones (LontaeJo***@discussions.microsoft.com) writes:
> I have a table with 3 columns SQL 2000 or SQL 2005?> > ProductID int, Product varchar(30), OrderDate datetime > > I have orders in this table from January to July I want to get a query > that will display how many orders I am getting per month and per week. I'm assuming SQL 2000, and you will get two queries: SELECT convert(char(6), OrderDate, 112) AS Month, COUNT(*) FROM Orders GROUP BY convert(char(6), OrderDate, 112) ORDER BY Month SELECT Monday, COUNT(*) FROM (SELECT dateadd(DAY, -(datepart(dw, OrderDate) + @@datefirst - 2) % 7, OrderDate) AS Monday FROM Orders) AS x GROUP BY Monday ORDER BY Monday -- Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx That works. WHat about per day and hour?
Show quote "Erland Sommarskog" wrote: > Lontae Jones (LontaeJo***@discussions.microsoft.com) writes: > > I have a table with 3 columns > > > > ProductID int, Product varchar(30), OrderDate datetime > > > > I have orders in this table from January to July I want to get a query > > that will display how many orders I am getting per month and per week. > > SQL 2000 or SQL 2005? > > I'm assuming SQL 2000, and you will get two queries: > > SELECT convert(char(6), OrderDate, 112) AS Month, COUNT(*) > FROM Orders > GROUP BY convert(char(6), OrderDate, 112) > ORDER BY Month > > SELECT Monday, COUNT(*) > FROM (SELECT dateadd(DAY, > -(datepart(dw, OrderDate) + @@datefirst - 2) % 7, > OrderDate) AS Monday > FROM Orders) AS x > GROUP BY Monday > ORDER BY Monday > > > -- > Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se > > Books Online for SQL Server 2005 at > http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx > Books Online for SQL Server 2000 at > http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx > Lontae Jones (LontaeJo***@discussions.microsoft.com) writes:
> That works. WHat about per day and hour? Now is the time for you to read about datetime function in Books Online,particularly datepart(). -- Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx |
|||||||||||||||||||||||