Home All Groups Group Topic Archive Search About

Count Customers FOr each month

Author
27 Jul 2006 9:34 PM
Lontae Jones
Hello,

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.

Author
27 Jul 2006 10:06 PM
Erland Sommarskog
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
Author
27 Jul 2006 10:20 PM
Lontae Jones
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
>
Author
28 Jul 2006 7:34 AM
Erland Sommarskog
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

AddThis Social Bookmark Button