如何在SQL Server中计算聚合产品函数 [英] How to Calculate Aggregated Product Function in SQL Server

查看:86
本文介绍了如何在SQL Server中计算聚合产品函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含2列的表:

I have a table with 2 column:

No.  Name    Serial
1    Tom       1
2    Bob       5
3    Don       3
4    Jim       6

我想添加一列内容要乘以以下的串行列:

I want to add a column whose a content is multiply Serial column like this:

No.  Name    Serial   Multiply
1    Tom       2         2
2    Bob       5         10
3    Don       3         30
4    Jim       6         180

我该怎么做?

推荐答案

哦,这是痛。大多数数据库不支持产品聚合功能。您可以使用日志和功能来模拟它。因此,这样的操作可能会起作用:

Oh, this is a pain. Most databases do not support a product aggregation function. You can emulate it with logs and powers. So, something like this might work:

select t.*,
       (select exp(sum(log(serial)))
        from table t2
        where t2.no <= t.no
       ) as cumeProduct
from table t;

请注意,可能会调用 log()在某些数据库中 ln()。同样,这适用于数字。有多种处理负数和零的方法,但这会使答案复杂化(示例数据都是正数)。

Note that log() might be called ln() in some databases. Also, this works for positive numbers. There are variations to handle negative numbers and zeroes, but this complicates the answer (and the sample data is all positive).

这篇关于如何在SQL Server中计算聚合产品函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆