Instruction
Perfect! It's time to wrap things up.
- To find the number of customers active within the last 30 days in weekly registration cohorts, use:
SELECT DATEPART(Year, RegistrationDate) AS Year, DATEPART(Week, RegistrationDate) AS Week, COUNT(*) AS ActiveCustomers FROM Customers WHERE DATEDIFF(Day, LastOrderDate, GETDATE()) < 30 GROUP BY DATEPART(Year, RegistrationDate), DATEPART(Week, RegistrationDate) ORDER BY DATEPART(Year, RegistrationDate), DATEPART(Week, RegistrationDate);
- To find the average order value in weekly registration cohorts, use:
SELECT DATEPART(Year, RegistrationDate) AS Year, DATEPART(Week, RegistrationDate) AS Week, AVG(TotalAmount) AS AverageOrderValue FROM Customers C JOIN Orders O ON C.CustomerID = O.CustomerID GROUP BY DATEPART(Year, RegistrationDate), DATEPART(Week, RegistrationDate) ORDER BY DATEPART(Year, RegistrationDate), DATEPART(Week, RegistrationDate);
- To find the number of "good customers" in weekly registration cohorts, use:
WITH GoodCustomers AS ( SELECT C.CustomerID, RegistrationDate, AVG(TotalAmount) AS AvgOrderValue FROM Customers C JOIN Orders O ON C.CustomerID = O.CustomerID GROUP BY C.CustomerID, RegistrationDate HAVING AVG(TotalAmount) > 1636 ) SELECT DATEPART(Year, RegistrationDate) AS Year, DATEPART(Week, RegistrationDate) AS Week, COUNT(*) AS GoodCustomers FROM GoodCustomers GROUP BY DATEPART(Year, RegistrationDate), DATEPART(Week, RegistrationDate) ORDER BY DATEPART(Year, RegistrationDate), DATEPART(Week, RegistrationDate);
How about a quick quiz before we move on to the next part?
Exercise
Click to continue.



