根据排名将行转换为列
Posted
技术标签:
【中文标题】根据排名将行转换为列【英文标题】:Converting rows into columns based on rank 【发布时间】:2020-05-08 13:04:07 【问题描述】:我正在尝试根据客户的购买日期(排名)将行转换为列。目标是找到客户购买的第一个、第二个、第三个、第四个和第五个产品类别。我也想知道客户购买了什么产品。
销售表
purchaseDate | productCategory | product | customer_id | customer_phonenumber | customer_email
2020-05-05 Electronics iPhone A001 1234567890 aoo1@abc.com
2020-05-06 Clothing T-shirt A001 1234567890 aoo1@abc.com
2020-05-07 Electronics Keyboard A001 1234567890 aoo1@abc.com
2020-05-08 Accessories iPhone Case A001 1234567890 aoo1@abc.com
结果
customer_id | customer_phoneNumber | customer_email | first_product_category | second_product_category | third_product_category | fourth_product_category | fifth_product_category | first_product | second_product | third_product | fourth_product | fifth_product
A001 1234567890 a001@abc.com Electonics Clothing Electronics Accessories NULL iPhone T-shirt Keyboard iPhone Case NULL
我想知道是否有任何其他方法可以做到这一点,因为我当前的查询花费了太长时间。
这是我的查询:
with ranked_order as (
select
purchaseDate
, productCategory
, product
, customer_id
, customer_phonenumber
, customer_email
, row_number() over(partition by productCategory order by purchaseDate desc) rank
from sales_table
)
select
customer_id
, customer_phonenumber
, customer_email
, max(case when rank = 1 then productCategory end) first_product_category
, max(case when rank = 2 then productCategory end) second_product_category
, max(case when rank = 3 then productCategory end) third_product_category
, max(case when rank = 4 then productCategory end) fourth_product_category
, max(case when rank = 5 then productCategory end) fifth_product_category
, max(case when rank = 1 then product end) first_product
, max(case when rank = 2 then product end) second_product
, max(case when rank = 3 then product end) third_product
, max(case when rank = 4 then product end) fourth_product
, max(case when rank = 5 then product end) fifth_product
from
ranked_order
group by 1,2,3
【问题讨论】:
我对你试图通过这个实现的东西感到困惑——我认为将这 10 列添加到每条购买记录中没有什么用处。你能提供一些样本数据和期望的结果吗?如果同一客户在同一天购买了多种产品会怎样? 嗨@GordonLinoff 我刚刚编辑了我的问题。希望它能提供更好的背景和清晰度。我刚刚意识到我在上一个问题中犯了一个错误。 【参考方案1】:以下是 BigQuery 标准 SQL
#standardSQL
SELECT
customer_id,
customer_phonenumber,
customer_email,
top5[SAFE_OFFSET(0)].productCategory AS first_product_category,
top5[SAFE_OFFSET(1)].productCategory AS second_product_category,
top5[SAFE_OFFSET(2)].productCategory AS third_product_category,
top5[SAFE_OFFSET(3)].productCategory AS fourth_product_category,
top5[SAFE_OFFSET(4)].productCategory AS fifth_product_category,
top5[SAFE_OFFSET(0)].product AS first_product,
top5[SAFE_OFFSET(1)].product AS second_product,
top5[SAFE_OFFSET(2)].product AS third_product,
top5[SAFE_OFFSET(3)].product AS fourth_product,
top5[SAFE_OFFSET(4)].product AS fifth_product
FROM (
SELECT customer_id, customer_phonenumber, customer_email,
ARRAY_AGG(STRUCT(productCategory, product) ORDER BY purchaseDate LIMIT 5) AS top5
FROM `project.dataset.sales_table`
GROUP BY customer_id, customer_phonenumber, customer_email
)
【讨论】:
【参考方案2】:我会将值放入数组中:
select customer_id, customer_phonenumber, customer_email,
array_agg(productCategory order by purchaseDate limit 5) as productCategorys_5,
array_agg(product order by purchaseDate limit 5) as products_5
from sales_table
group by 1,2,3
这会将值放入数组而不是列中。如果这具有足够好的性能,您可以只索引数组以获取单个元素(尽管我可能会发现数组更方便)。
【讨论】:
以上是关于根据排名将行转换为列的主要内容,如果未能解决你的问题,请参考以下文章