简体   繁体   English

在SQL中计算XIRR

[英]Calculating XIRR in SQL

I have been trying to find a solution to calculate XIRR in SQL (TSQL). 我一直在尝试找到一种在SQL(TSQL)中计算XIRR的解决方案。 Ideally, the values would match the values that Excel calculates using the same input data. 理想情况下,这些值应与Excel使用相同输入数据计算的值匹配。 I have found several suggested solutions around the web but all seem to be flawed in some manner. 我在网上找到了一些建议的解决方案,但似乎都存在某种缺陷。 Below is an example that we have been using that works in almost all cases. 下面是我们一直在使用的几乎在所有情况下都有效的示例。

CREATE FUNCTION [dbo].[CalcXIRR]
(
    @Sample XIRRTable READONLY,
    @Rate DECIMAL(19, 9) = 0.1
)
RETURNS DECIMAL(38, 9)
AS
BEGIN
    DECLARE @X DECIMAL(19, 9) = 0.0,
    @X0 DECIMAL(19, 9) = 0.1,
    @f DECIMAL(19, 9) = 0.0,
    @fbar DECIMAL(19, 9) = 0.0,
    @i TINYINT = 0,
    @found TINYINT = 0

IF @Rate IS NULL
    SET @Rate = 0.1

SET @X0 = @Rate

WHILE @i < 100
    BEGIN
        SELECT  @f = 0.0,
            @fbar = 0.0

        SELECT      @f = @f + value * POWER(1 + @X0, (-theDelta / 365.0E)),
        @fbar = @fbar - theDelta / 365.0E * value * POWER(1 + @X0, (-theDelta / 365.0E - 1))
        FROM    (
                SELECT  Value,
                    DATEDIFF(DAY, MIN(date) OVER (), date) AS theDelta
                FROM    @Sample
            ) AS d

        SET @X = @X0 - @f / @fbar

        If ABS(@X - @X0) < 0.00000001
        BEGIN
           SET @found = 1
           BREAK;
        END

        SET @X0 = @X
        SET @i += 1
   END

 If @found = 1
    RETURN  @X

RETURN NULL
END

GO

However, with the data below, 但是,根据以下数据,

在此处输入图片说明

we are getting an error 我们遇到错误

An invalid floating point operation occurred.

This is happening on the line 这正在发生

SELECT @f = @f + value * POWER(1 + @X0, (-theDelta / 365.0E))

It essentially gets down to a specific calculation 它实质上取决于特定的计算

POWER(-0.635634780,-0.0849315)

Could it be that there is some simple syntax adjustment that could fix this error or is the function itself not going to work? 可能是有些简单的语法调整可以解决此错误,或者函数本身不起作用? Excel seems to handle this calculation without a problem. Excel似乎可以毫无问题地处理此计算。

This is just one of many examples I have tried using. 这只是我尝试使用的许多示例之一。 I can break down another example, if needed. 如果需要,我可以分解另一个示例。 I can edit the post to make it fit Stack Overflow's standard with guidance. 我可以编辑该帖子,使其在指导下适合Stack Overflow的标准。 I find it very unusual that there is no clear discussion on how to calculate XIRR in SQL. 我发现非常罕见的是,没有关于如何在SQL中计算XIRR的明确讨论。 Every solution seems to have problems yet Excel spits out values so effortlessly. 每个解决方案似乎都有问题,但Excel毫不费力地吐出了值。

Assuming your image is in A1 at the top left. 假设图像在左上角的A1中。 Does not solve your SQL but I hope may be of some help: 无法解决您的SQL,但希望对您有所帮助:

Firstly, my Excel also returns -0.6719218 . 首先,我的Excel也返回-0.6719218

Secondly, Excel's calculation is detailed at XIRR function from which part is: 其次,在XIRR函数中详细介绍了Excel的计算,其中包括:

XIRR公式

However, that is for a series of cash flows, for which Excel uses iteration, and your specific example is merely a pair of paired entries, for which the soluble equation may be expressed as: 但是,这是针对一系列现金流量的,Excel使用其进行迭代,并且您的特定示例只是一对成对的条目,其可溶方程式可以表示为:

=10^(LOG(-A2/A1)*365/(B2-B1))

This part: 365/(B2-B1) is the reciprocal of your 0.0849315 , so it may be worth investigating your 0.635634780 more closely (I'm not clear how you arrived at that). 这部分: 365/(B2-B1)是您的0.0849315的倒数,因此可能值得更仔细地研究您的0.635634780 (我不清楚您是如何得出的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM