R prod() 函数,但在 SQL 中,如何通过

R prod() function but in SQL, how to take product of a group by

在R中,取一组的乘积可以像这样进行:

 iris %>% group_by(Species) %>% summarize(new_col = prod(Petal.Length))

如何在 postgresql 或 dbplyr/dplyr 中实现相同的概念?

不幸的是,SQL 标准没有定义聚合“产品”函数。但是,您可以使用算术解决此问题。

假设您要在 table 中共享相同 species 的行组中计算 petal_length 的乘积 mytable:

select species, exp(sum(ln(petal_length))) petal_length_product
from mytable
group by species

只要 petal_length 的所有值都大于 0