Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compute cumulative product in SQL Server 2008?

I have below table with 2 columns, DATE & FACTOR. I would like to compute cumulative product, something like CUMFACTOR in SQL Server 2008.

Can someone please suggest me some alternative.enter image description here

like image 918
Prav Avatar asked Mar 01 '16 08:03

Prav


People also ask

How do you calculate cumulative in SQL?

In SQL server also you can calculate cumulative sum by using sum function. We can use same table as sample table. select dept_no Department_no, count(empno) Employee_Per_Dept, sum(count(*)) over (order by deptno) Cumulative_Total from [DBO].

How do you calculate cumulative percentage in SQL Server?

The subquery SELECT SUM(Sales) FROM Total_Sales calculates the sum. We can then divide the running total, "SUM(a2. Sales)", by this sum to obtain the cumulative percent to total for each row.

What is cumulative product?

A cumulative product is a sequence of partial products of a given sequence.


1 Answers

Unfortunately, there's not PROD() aggregate or window function in SQL Server (or in most other SQL databases). But you can emulate it as such:

SELECT Date, Factor, exp(sum(log(Factor)) OVER (ORDER BY Date)) CumFactor
FROM MyTable
like image 140
Lukas Eder Avatar answered Nov 04 '22 07:11

Lukas Eder