Great! We now want to create a conversion chart. This report should show weekly registration cohorts in rows and the number of conversions (i.e., first purchases) within one week, two weeks, etc. We want the end report to look like this:
Year |
Week |
RegisteredCount |
NoSale |
OneWeek |
TwoWeeks |
OverTwoWeeks |
2016 |
7 |
20 |
0 |
20 |
0 |
0 |
2016 |
8 |
14 |
0 |
14 |
0 |
0 |
2016 |
9 |
10 |
0 |
9 |
1 |
0 |
... |
... |
... |
... |
... |
... |
... |
Here's the query:
SELECT
DATEPART(Year, RegistrationDate) AS Year,
DATEPART(Week, RegistrationDate) AS Week,
COUNT(*) AS RegisteredCount,
COUNT(CASE WHEN FirstOrderId IS NULL THEN CustomerID END) AS NoSale,
COUNT(CASE WHEN DATEDIFF(Day, RegistrationDate, FirstOrderDate) < 7 THEN CustomerID END) AS OneWeek,
COUNT(CASE WHEN DATEDIFF(Day, RegistrationDate, FirstOrderDate) >= 7
AND DATEDIFF(Day, RegistrationDate, FirstOrderDate) < 14 THEN CustomerID END) AS TwoWeeks,
COUNT(CASE WHEN DATEDIFF(Day, RegistrationDate, FirstOrderDate) >= 14 THEN CustomerID END) AS OverTwoWeeks
FROM Customers
GROUP BY
DATEPART(Year, RegistrationDate),
DATEPART(Week, RegistrationDate)
ORDER BY
DATEPART(Year, RegistrationDate),
DATEPART(Week, RegistrationDate);
As usual, we used DATEPART()
to extract the year and week of registration. Note that we also used the COUNT(CASE WHEN...)
technique – this way, we could include multiple metrics in a single report.