SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 EXISTS Query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Oliviaf2012
Starting Member

16 Posts

Posted - 08/01/2012 :  17:41:53  Show Profile  Reply with Quote
I am trying to write an exists query to show me new customers.
I want to return the number of customers who bought this month but never bought before - anyone know how to do that ? There is other criteria the customers need to meet ( the brand they purchased, their country etc)

visakh16
Very Important crosS Applying yaK Herder

India
47189 Posts

Posted - 08/01/2012 :  19:08:53  Show Profile  Reply with Quote

SELECT *
FROM Table t
WHERE BuyDate > = DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)
AND BuyDate < DATEADD(mm,DATEDIFF(mm,0,GETDATE())+1,0)
AND NOT EXISTS (SELECT 1 FROM table WHERE CustomerID = t.CustomerID AND BuyDate < DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)
                AND other conditions...)
AND other conditions....


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Oliviaf2012
Starting Member

16 Posts

Posted - 08/02/2012 :  07:04:49  Show Profile  Reply with Quote
Fantastic visakh16 that worked beautifully :)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47189 Posts

Posted - 08/02/2012 :  10:13:37  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000