Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Selecting a customer's first purchase date Expand / Collapse
Author
Message
Posted Friday, November 13, 2009 6:09 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, August 15, 2014 5:03 AM
Points: 43, Visits: 363
Hi Guys, need some help here.. My manager has asked me to write a query that will return all customers who have bought for the first time the previous day. Seems pretty simple but so far I've not been able to nail it, it just returns all the customers who bought the previous day not only those who's first purchase was the previous day. Can someone tell me where I'm going wrong

SELECT COUNT(DISTINCT dbo.orders.user_id) AS Users_Amount, 
MIN(dbo.orders.created_date) AS First_Purchase_Date
FROM dbo.orders
WHERE (dbo.orders.created_date >= DATEADD(DAYOFYEAR, - 1, GETDATE()))
AND (dbo.orders.base_price > 0)

Post #818433
Posted Friday, November 13, 2009 6:31 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Tuesday, August 12, 2014 1:04 PM
Points: 531, Visits: 431
This gives you a detail list:

SELECT F.[user_id], F.First_Purchase_Date
FROM (
SELECT [user_id],
MIN(dbo.orders.created_date) AS First_Purchase_Date
FROM dbo.orders
WHERE base_price > 0
GROUP BY [user_id]) F
WHERE F.First_Purchase_Date >= DATEADD(DAYOFYEAR, - 1, GETDATE())



Post #818447
Posted Friday, November 13, 2009 7:55 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, August 15, 2014 5:03 AM
Points: 43, Visits: 363
Thanks dude, works now. Never though of doing a select in the FROM, that's where experience comes in handy, cheers!
Post #818515
Posted Sunday, November 15, 2009 1:46 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, September 11, 2014 1:58 AM
Points: 2,397, Visits: 3,407
SELECT		COUNT(*) AS User_Count
FROM (
SELECT [User_ID],
MIN(Created_Date) AS First_Purchase_Date
FROM dbo.Orders
GROUP BY [User_ID]
HAVING MIN(Created_Date >= DATEDIFF(DAY, 1, GETDATE())
AND MIN(Created_Date < DATEDIFF(DAY, 0, GETDATE())
) AS d




N 56°04'39.16"
E 12°55'05.25"
Post #819153
Posted Monday, November 16, 2009 6:56 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 1:29 PM
Points: 11,286, Visits: 13,072
I'd try something like this:

SELECT
O.user_id
FROM
dbo.orders AS O
GROUP BY
O.user_id
HAVING
MIN(created_date) >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE())-1, 0) AND
MIN(created_date) < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)

I'd also test the other solutions. The only issue I would have with the other solutions is that they do not take into account the time portion of the day, if you are using the new DATE datatype in SQL Server 2008 that is not an issue, but with datetime or smalldatetime it will be an issue, and they will give you the anyone with a first order date of yesterday or today, not just yesterday which was the request.

I tested the solutions using my copy of AdventureWorks and they evaluate out to the same execution plan on on my desktop (dual core).




Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #819351
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse