如何在SQL Server中计算聚合产品函数

编程入门 行业动态 更新时间:2024-10-11 05:30:07
本文介绍了如何在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中计算聚合产品函数

本文发布于:2023-11-22 01:37:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615468.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   如何在   产品   Server   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!