1

Given a table with monthly transactions (customer id, month, payment) and a table with customer info (type 2 dimension) (id, cust_id, plan type, num users, start_date, end_date):

What is the top grossing plan each month (month, $, plan)?

My answer below seems like it would only return the top products plan by amount rather than per month.

SELECT 
    Sales.month as SalesMonth, 
    SUM(Sales.payment) AS MonthlySales, 
    CustomerInfo.plan_type AS PlanType 
FROM Sales 
INNER JOIN CustomerInfo ON Sales.customer_id=CustomerInfo.cust_id
GROUP BY SalesMonth, MonthlySaleS, PlanType 
ORDER BY MonthlySales, PlanType
ORDER BY MonthlySales DESC 
LIMIT 1

I am stumped on the next two.

2) Given the above tables how many customers are brought on every month (month, plan, # new customers)?

3) Given the above tables, how many people switch plans per month (month, from plan to plan, # customers)?

GMB
  • 216,147
  • 25
  • 84
  • 135
Drew Pham
  • 89
  • 7

1 Answers1

2

You could proceed as follows:

  • first use an aggregate query to compute the monthly sales per plan
  • then rank records by descending monthly sales within months partitions
  • finally, filter on the top record in each month

Query:

SELECT SalesMonth, PlanType, MonthlySales
FROM (
    SELECT 
        x.*, 
        ROW_NUMBER() OVER(PARTITION BY as SalesMonth ORDER BY MonthlySales desc) rn
    FROM (
        SELECT 
            s.month as SalesMonth, 
            c.plan_type AS PlanType, 
            SUM(s.payment) AS MonthlySales
        FROM sales s
        INNER JOIN CustomerInfo s ON s.customer_id = c.cust_id
        GROUP BY s.month, c.plan_type
    ) x
) y
WHERE rn = 1
GMB
  • 216,147
  • 25
  • 84
  • 135
  • what is the `y` doing? how are you filtering on the top record in each month? thanks for the assistance! – Drew Pham Sep 28 '19 at 22:05
  • @DrewPham: `y` is an alias for the derived table that is generated by the subquery. The finale `WHERE` clause fiters on the top record of each month. – GMB Sep 28 '19 at 22:23
  • Sorry new to stack overflow how would I be able to find how many new customers are brought on every month (month, plan, # new)? – Drew Pham Sep 29 '19 at 18:52