久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

計算累積產(chǎn)品價值

Calculate cumulative product value(計算累積產(chǎn)品價值)
本文介紹了計算累積產(chǎn)品價值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有以下數(shù)據(jù)庫表:

Date        Return  Index
01-01-2020  0.1     Null 
01-02-2020  0.2     Null
01-03-2020  0.3     Null

我想使用以下公式更新索引值:

I would like to update the Index value using the following formula:

Index = (Previous_Month_Index * Return) + Previous_Month_Index (Use 100 for Previous_Month_Index for the first month)

預(yù)期結(jié)果:(按日期升序計算的索引)

Expected Result: (Index to be calculated order by Date asc)

Date        Return  Index
01-01-2020  0.1     110  -- (100 + 10)
01-02-2020  0.2     132  -- (110 + (110 * 0.20)) = 110 + 22 = 132
01-03-2020  0.3     171.6  -- (132 + (132 * 0.30)) = 132 + 39.6 = 171.6

如何使用 SQL 執(zhí)行此操作?我嘗試了以下查詢,但出現(xiàn)錯誤:

How can I do this using SQL? I tried the following query but getting an error:

窗口函數(shù)不能在另一個窗口函數(shù)或聚合的上下文中使用.

Windowed functions cannot be used in the context of another windowed function or aggregate.

--first, load the sample data to a temp table
select *
into #t
from 
(
  values
  ('2020-01-01', 0.10),
  ('2020-02-01', 0.20),
  ('2020-03-01', 0.30)
) d ([Date], [Return]);

--next, calculate cumulative product
select *, CumFactor = cast(exp(sum(log(case when ROW_NUMBER() OVER(order by [Date] ASC)  = 1 then 100 * [Return] else [Return] end)) over (order by [Date])) as float) from #t;

drop table #t

推薦答案

從數(shù)學上來說,你想要的結(jié)果相當于這個產(chǎn)品:

Thinking mathematically, the result that you want is equivalent to this product:

100 * (1 + a1) * (1 + a2) * (1 + a3) * ....

其中 a1、a2、a3 是 [Return] 列的值.

where a1, a2, a3 are the values of the column [Return].

該產(chǎn)品可以通過以下方式獲得:

This product can be obtained by:

100 * EXP(SUM(LOG(1 + [Return])))

你可以在 sql 中這樣做:

and you can do this in sql like this:

SELECT *, 
       100 * EXP(SUM(LOG(1 + [Return])) OVER (ORDER BY [Date])) [Index]
FROM #t

請參閱演示.

這篇關(guān)于計算累積產(chǎn)品價值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉(zhuǎn)換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創(chuàng)建視圖?)
Create calculated value based on calculated value inside previous row(根據(jù)前一行內(nèi)的計算值創(chuàng)建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發(fā)技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉(zhuǎn)換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 亚色视频| 午夜丁香 | 奇米av | 欧美日韩视频在线 | 岛国免费av | 久久国产精 | 在线日韩欧美 | 美日韩一区二区三区 | 黄色xxxxx | 日本少妇高潮达到高潮 | 色哟哟入口国产精品 | 天天综合色 | 日韩专区在线 | 天天操夜| 亚洲精品日韩丝袜精品 | 午夜天堂在线 | 日韩网站免费观看 | 亚洲精品1区| 亚洲一区二区免费 | 在线视频h | 久久精品一区二区三区四区 | 天天干天天操天天摸 | 亚洲激情另类 | 一级片免费 | 欧美一级做性受免费大片免费 | 中文字幕精品三区 | 久久香蕉精品 | 四虎com| 成人网在线观看 | 九色精品 | av入口 | 成人黄色大片 | 亚洲一区视频在线 | 手机av免费| www.麻豆av| 中文字幕一区二区三区在线观看 | 中文字幕在线观看一区二区三区 | 亚洲天堂av网| 午夜视频在线播放 | 青青草网站 | 黄色一级视频免费看 |