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

計(jì)算累積產(chǎn)品價(jià)值

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

問(wèn)題描述

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

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é)果:(按日期升序計(jì)算的索引)

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í)行此操作?我嘗試了以下查詢(xún),但出現(xiàn)錯(cuò)誤:

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

窗口函數(shù)不能在另一個(gè)窗口函數(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ù)學(xué)上來(lái)說(shuō),你想要的結(jié)果相當(dāng)于這個(gè)產(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)品可以通過(guò)以下方式獲得:

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

請(qǐng)參閱演示.

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

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

相關(guān)文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個(gè)子標(biāo)記轉(zhuǎn)換為具有多個(gè)分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個(gè)表創(chuàng)建視圖?)
Create calculated value based on calculated value inside previous row(根據(jù)前一行內(nèi)的計(jì)算值創(chuàng)建計(jì)算值)
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?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對(duì)?) - IT屋-程序員軟件開(kāi)發(fā)技
Recursive t-sql query(遞歸 t-sql 查詢(xún))
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱(chēng)轉(zhuǎn)換為日期/月份編號(hào)(問(wèn)題和答案的組合))
主站蜘蛛池模板: 一区二区三区欧美 | 成人国产精品 | 亚洲精品国产偷自在线观看 | 久久精品国产99国产 | 成人亚洲 | 天天插天天狠天天透 | 小川阿佐美pgd-606在线 | 超碰日韩 | 亚洲综合在线播放 | 国产乱肥老妇国产一区二 | 日韩在线观看网站 | 欧美精品久久久久 | 欧美一区二不卡视频 | 久久er99热精品一区二区 | 中文二区 | 精品综合久久 | 成人精品一区二区三区四区 | 天天看天天摸天天操 | 国产日韩免费视频 | 久久久久久高潮国产精品视 | 日韩av免费在线电影 | 亚洲自拍一区在线观看 | 亚洲一区二区三区在线播放 | 一级特黄在线 | 奇米四色在线观看 | 有码一区 | 免费在线观看成人 | 久久综合伊人 | 欧美一区2区三区4区公司二百 | 精品福利在线 | 国产日韩精品视频 | 三极网站| 日韩在线播放第一页 | 免费国产黄网站在线观看视频 | 伊人网站在线 | 国产高清在线精品 | 午夜影晥 | 国产精品成人久久久久 | 国产精品欧美一区二区 | 亚洲精品高清视频在线观看 | 久久最新|