【发布时间】:2020-01-28 17:19:28
【问题描述】:
给定一个包含每月交易(客户 ID、月份、付款)的表格和一个包含客户信息(类型 2 维度)(id、cust_id、计划类型、用户数量、开始日期、结束日期)的表格:
每月收入最高的计划是什么(月、美元、计划)?
我在下面的回答似乎只会按数量而不是按月返回顶级产品计划。
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
我被接下来的两个难住了。
2) 鉴于上表,每个月带来多少客户(月、计划、# 个新客户)?
3) 鉴于上述表格,每月有多少人转换计划(每月,从计划到计划,# 个客户)?
【问题讨论】:
标签: sql inner-join aggregate-functions greatest-n-per-group