sql - 基于 Pyspark 中其他值的一列的条件聚合
问题描述
尽管我在类似的行中看到了一个问题,但这不符合我的目的。所以把它贴在这里,期待得到答案。我有 2 个 pyspark 数据框:客户每周在产品上的花费
我需要根据产品的第一周和最后一周的销售情况来获取客户在每个产品对应的类别中的支出。例如对应产品 W,因为它只在第 2 周出现,所以我只需要考虑客户在第 2 周的类别支出
我正在努力解决它,但没有运气。寻找建议。
解决方案
我想我明白了。这比我想象的要复杂,但我认为这可以满足您的要求:
select t1.*, t2.category_spend
from table1 t1 join
(select t2.product, sum(t1.spend) as category_spend
from table1 t1 join
table2 t2
on t1.week between t2.weekstart between t2.weekend
group by t2.product
) t2w
on t2w.product = t1.product;
编辑:
根据您的评论,逻辑基本相同。这个customer
问题并没有区分它,所以它可以被包含在“任何地方”:
select t1.*, t2.category_spend
from table1 t1 join
(select t1.customer, t2.product, sum(t1.spend) as category_spend
from table1 t1 join
table2 t2
on t1.week between t2.weekstart between t2.weekend
group by t2.product, t1.customer
) t2w
on t2w.product = t1.product and t2w.customer = t1.customer;
推荐阅读
- python - Django 加载 20,000 个 QuerySet 的速度快多少?
- struct - Octave:结构体和元胞数组之间的差异
- asp.net-mvc - 会话超时后出现 403 错误,现在无法登录或注销
- c# - 通过
从字符串参数使用 AddSingleton - c - 如何使用 C 中的函数指针遍历泛型类型 (void**) 的数组?
- c++ - 使用类作为枚举类,或具有特定值
- postgresql - 我有以下字符串'3,45,543,6,89'。需要像表格一样的输出功能。请帮助我通过 postgresql 函数获取输出
- matrix - 多个 tablix 中的隐藏标题行导致空白
- alpha-vantage - 为 NSE 交易品种代码调用 AlphaVantage API 时返回空数组
- php - php 5.3/mysql:为什么 apache 看不到 mysql?