Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Final Quiz
4. Question 3

Instruction

Good job! Now let's take a look at customer activity.

Exercise

Find the number of "good customers" in weekly signup cohorts from the first quarter of 2017. Define a good customer as one whose average total order amount was above $1450.00. Show the following columns:

  • year – the year of registration.
  • week – the week of registration.
  • percent_of_good_customers – the percent of good customers.

Order the results by year and week.

Stuck? Here's a hint!

First, you will need to create a CTE that returns the table of customer IDs, their registration dates, and their average order value. In the inside query:

  • Join the customers and orders tables.
  • Create three columns: customer_id, registration_date, AVG(total_amount).
  • Group rows by the customer_id and registration_date columns.

In the outer query:

  • Using the DATE_PART() function to extract the year and week.
  • Using the COUNT() function and the CASE WHEN statement, count the percentage of the "good customers," like so:
    COUNT(CASE WHEN /* only values over 1450 */ END) * 100.0 / COUNT(/* all values */) AS percent_of_good_customers
  • Don't forget about grouping the result by the year and week.