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
 General SQL Server Forums
 New to SQL Server Programming
 Very Urgent Issue

Author  Topic 

Bill13
Starting Member

2 Posts

Posted - 2009-03-01 : 18:17:41
i have 5 years of data in a sales table now my manager want the customer who is new customer of our company and first time made a purchase in the month of february 2009.

mean the customer is new and 1st time he bought somthing in the month of feb.

Select Customer_Name
from Sales
where sale_date = ??????????????????

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2009-03-01 : 18:44:53
[code]Select Customer_Name
from Sales
where sale_date >= '2009-02-01' and sales_date < '2009-03-01'[/code]but...
That will give you not just new customers who made their first purchase in Feb 2009, but ALL customers who bought something in that month. Also, it will give you Customer_name several times if the same customer made several purchases in Feb.
so...
[code]Select distinct Customer_Name
from Sales s1
where sale_date >= '2009-02-01' and sales_date < '2009-03-01'
and not exists( select distinct Customer_Name from Sales s2 where s1.Customer_Name = s2.Customer_Name and s2.sales_date < '2009-02-01')[/code]Now, I am assuming that Customer_Name is unique or your primary key or something. Otherwise, you will be in trouble if you had two customers who had the same name.
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2009-03-01 : 21:03:52
You could also use:

SELECT Customer_Name FROM Sales
GROUP BY Customer_Name
HAVING Min(sales_date) BETWEEN '2/1/2009' and '2/28/2009'
Go to Top of Page
   

- Advertisement -