问题描述
我的数据集如下(前四列)-
Person Rate Count isImportant Results
A $2 15 true $2
B $1 30 true $1
B $3 10 false (3*10+2.5*25)/(10+25)
B $2.5 25 false (3*10+2.5*25)/(10+25)
D $1.5 20 false $1.5
如何通过将列Results
和Person
分组并计算isImportant
和Rate
的加权平均值来生成第五列Count
列?
解决方法
您可以使用窗口功能:
select t.*,sum(rate * count) over(partition by person,isImportant)
/ sum(count) over(partition by person,isImportant) result
from mytable t