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

計算累積產品價值

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

問題描述

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

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)

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

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ù)學上來說,你想要的結果相當于這個產品:

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].

該產品可以通過以下方式獲得:

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

請參閱演示.

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

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

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-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(根據前一行內的計算值創(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)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 色爱区综合 | 国产线视频精品免费观看视频 | 亚洲欧美网站 | 亚洲成人网在线播放 | 天天碰日日操 | 国产不卡在线观看 | 欧洲妇女成人淫片aaa视频 | 夜夜操天天干 | 欧美一区二区三区大片 | 视频在线一区二区 | 日韩精品一区二区三区中文字幕 | 中文在线播放 | 精品国产免费一区二区三区五区 | 日韩一级免费 | 日韩在线小视频 | www.av在线| 91久久久精品国产一区二区蜜臀 | 超碰97免费| 国产精品综合久久 | 日韩图区 | 91五月天| 亚洲精品久久久久国产 | 欧美国产91 | 久久免费香蕉视频 | 亚洲电影一区二区三区 | 欧美日本在线观看 | 亚洲综合色自拍一区 | 成人久久 | 在线一区二区观看 | 午夜a级理论片915影院 | 国产夜恋视频在线观看 | 精品国产91乱码一区二区三区 | 在线视频一区二区三区 | 99在线免费观看视频 | 亚洲精品国产第一综合99久久 | 中文字幕一区二区三区精彩视频 | 成人免费视频网站在线看 | 日本视频免费观看 | 欧美视频一区二区三区 | 少妇一区在线观看 | 欧美午夜一区二区三区免费大片 |