177. Purchases by platform report

medium amazon

****************************************************************

*************************************************************************************************, **********************.

Table: orders

An eCommerce company's online order table.

  col_name    | col_type
--------------+-------------------
order_id      | bigint
product_id    | bigint
customer_id   | bigint
order_dt      | date
qty           | integer
unit_price_usd| float
channel       | varchar(20) -- mobile, desktop

Sample results

 platform_category | num_customers |       spend
-------------------+---------------+--------------------
 both              |            30 | 185455.25999999992
 desktop_only      |            15 |  96647.46999999994
 mobile_only       |             5 |  33410.03999999997

Solution postgres

WITH customer_spend AS (
    SELECT customer_id,
        SUM(CASE WHEN channel = 'mobile' THEN qty * unit_price_usd ELSE 0 END) mobile_spend,
        SUM(CASE WHEN channel = 'desktop' THEN qty * unit_price_usd ELSE 0 END) desktop_spend
    FROM orders
    GROUP BY customer_id
)

SELECT CASE WHEN mobile_spend >0 AND desktop_spend =0 THEN 'mobile_only'
         WHEN mobile_spend =0 AND desktop_spend >0 THEN 'desktop_only'
         WHEN mobile_spend >0 AND desktop_spend >0 THEN 'both'
    END AS platform_category,
    COUNT(DISTINCT customer_id) AS num_customers,
    SUM(mobile_spend + desktop_spend) AS spend
FROM customer_spend
GROUP BY 1;
    

Explanation

This query is used to categorize customers based on their spending behavior across different platforms (mobile and desktop).

The first part of the query creates a temporary table called "customer_spend" using a subquery. This subquery calculates the total spend of each customer on mobile and desktop channels separately using a conditional statement.

The second part of the query uses the results from the temporary table to categorize customers into three categories: "mobile_only", "desktop_only", and "both". The query then calculates the total number of customers in each category and their total spend across both platforms.

Finally, the results are grouped by the platform category using the GROUP BY clause. This query can help a data analyst understand the distribution of customers based on their platform spending and identify any patterns or trends in customer behavior.

Expected results


More Amazon questions

ID Title Level FTPR
93 number of sales for each product this month amazon easy
20%
94 Top 3 products vs. bottom 3 products amazon medium
7%
95 Candidate products for subscription amazon hard
13%
108 Free premium membership amazon hard
16%
109 Top 3 most popular product categories amazon easy
11%
110 Most popular product by category amazon medium
12%
111 Second most popular product amazon medium
16%
112 Top 3 customers amazon easy
30%
113 Daily cumulative spend amazon easy
11%
114 Dod revenue change rate amazon easy
15%
115 Rolling average revenue amazon hard
28%
116 Top answers day by device amazon easy
14%
117 Rolling 7 days total answers amazon easy
35%
175 Employees' email account amazon easy
14%
176 Employees' annual bonus amazon easy
12%