SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Selecting a customer's first purchase date


Selecting a customer's first purchase date

Author
Message
romanoplescia
romanoplescia
Right there with Babe
Right there with Babe (763 reputation)Right there with Babe (763 reputation)Right there with Babe (763 reputation)Right there with Babe (763 reputation)Right there with Babe (763 reputation)Right there with Babe (763 reputation)Right there with Babe (763 reputation)Right there with Babe (763 reputation)

Group: General Forum Members
Points: 763 Visits: 433
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)


peterhe
peterhe
SSCarpal Tunnel
SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)

Group: General Forum Members
Points: 4940 Visits: 453
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())



romanoplescia
romanoplescia
Right there with Babe
Right there with Babe (763 reputation)Right there with Babe (763 reputation)Right there with Babe (763 reputation)Right there with Babe (763 reputation)Right there with Babe (763 reputation)Right there with Babe (763 reputation)Right there with Babe (763 reputation)Right there with Babe (763 reputation)

Group: General Forum Members
Points: 763 Visits: 433
Thanks dude, works now. Never though of doing a select in the FROM, that's where experience comes in handy, cheers! :-D
SwePeso
SwePeso
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16643 Visits: 3433

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"
Jack Corbett
  Jack Corbett
SSC Guru
SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)

Group: General Forum Members
Points: 78606 Visits: 14969
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
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search