我有一个包含以下标头的数据集:product, sales, material_1, material_2, material_3, style_1, style_2, style_3。
基于这个数据集,我需要找到基于销售的最成功的材料和风格。列material_1应该加权*3,material_2应该加权*2,material_3应该加权*1。style列也是如此。
因此,基本上,我需要以某种方式对每一列进行不同的加权,然后将其乘以该产品的销售额,但我很难弄清楚如何做到这一点。根据我对数据的看法,棉花材料和现代风格应该是最成功的结果。
Product Sales material_1 material_2 material_3 style_1 style_2 style_3
A 2629715 Cotton Synthetic Silk modern vintage
B 2527075 Cotton leather Synthetic modern Young
C 678434 Cotton Synthetic Silk modern Punk
D 735281 Cotton leather Wool modern vintage
E 439256 Cotton Silk Denim young
F 353630 Silk Denim Cotton Punk
G 579318 Wool leather Cotton vintage Young Chic
H 505876 Cotton leather Wool modern young
I 325997 Cotton Silk contemporary modern
J 274980 Cotton Silk young
K 280422 Denim young Punk Punk
L 293283 Synthetic Cotton modern
M 217215 Cotton Silk young modern
N 306687 Synthetic Wool Cotton modern vintage
O 232490 Cotton Silk modern young
P 216559 Synthetic Cotton modern
Q 219996 Denim Cotton young发布于 2019-10-02 22:21:26
一些“预期输出”将是有帮助的,但如果没有,这里有一种方式来考虑它:
WITH materials(weight, material) AS (
SELECT 3 AS weight, material_1 AS material from mytable
UNION ALL
SELECT 2, material_2 from mytable
UNION ALL
SELECT 1, material_3 from mytable
)
SELECT material, SUM(weight) AS Weighted_Value
FROM materials
GROUP BY material下面是它的功能:
material列移动到单个列中,以便您可以更轻松地处理它们分组
您可以对style字段使用类似的逻辑。试一试,让我知道它是否适用于您。
https://stackoverflow.com/questions/58202658
复制相似问题