0% found this document useful (0 votes)
9 views

SQL Advanced DATE Function_ DATEDIFF

SQL functions

Uploaded by

iammayank965
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

SQL Advanced DATE Function_ DATEDIFF

SQL functions

Uploaded by

iammayank965
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

DATEDIFF()

Problem Statement: As a data analyst in an eCommerce company, you want to


analyze customer purchase behavior by calculating the number of days
between each customer’s first and most recent purchase. This will help
identify customers who are becoming less active and inform retention
strategies.

linkedin.com/in/ileonjose
SQL
DATEDIFF()

BREAKDOWN:

SELECT Statement: Retrieves CustomerID, the minimum (MIN) purchase


date as FirstPurchaseDate, and the maximum (MAX) purchase date as
LastPurchaseDate.

DATEDIFF Function: Calculates the difference in days between the most


recent and the first purchase using DATEDIFF(MAX(PurchaseDate),
MIN(PurchaseDate)), and aliases it as DaysBetweenPurchases.

FROM Clause: Specifies the Purchases table, which contains the purchase
history of customers.

GROUP BY Clause: Groups the results by CustomerID to compute the first


and last purchase dates for each customer.

HAVING Clause: Filters the results to include only customers whose


purchase interval exceeds 30 days.

linkedin.com/in/ileonjose
SQL

You might also like