Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8421623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:11:57+00:00 2026-06-10T03:11:57+00:00

I have a data table like this: Group ID M0 M1 … M20 ————————————

  • 0

I have a data table like this:

Group   ID    M0    M1    ...    M20
------------------------------------
    1    1    10    -2    ...     54
    1    2    -8    10    ...    -20
    2    6   -10    12    ...      0
    2    4    45    52    ...      0
    2    5    12   102    ...      0

I’m grouping my data by Group like so:

SELECT Group
     , round(sum(M0+M1+M2+...+M20)/count(ID),2) as profit
     , round(sum(M0),2) as M0
     , round(sum(M1),2) as M1
     , ...
     , round(sum(M20),2) as M20
FROM
    data_table
GROUP BY
    Group

What I need to add is IRR based on M0 to M20 columns.

I found an IRR function that looks like this:

CREATE FUNCTION [dbo].[ufn_IRR]
  (
   @strIDs VARCHAR(8000),
   @guess DECIMAL(30,10)
  )
RETURNS DECIMAL(30, 10)
AS 
  BEGIN
    DECLARE @t_IDs TABLE (
        id INT IDENTITY(0, 1),
        value DECIMAL(30, 10)
    )
    DECLARE @strID VARCHAR(12),@sepPos INT,@NPV DECIMAL(30, 10)
    SET @strIDs = COALESCE(@strIDs + ',', '')
    SET @sepPos = CHARINDEX(',', @strIDs)
    WHILE @sepPos > 0 
      BEGIN
        SET @strID = LEFT(@strIDs, @sepPos - 1)
        INSERT INTO @t_IDs ( value ) SELECT ( CAST(@strID AS DECIMAL(20, 10)) ) WHERE ISNUMERIC(@strID) = 1
        SET @strIDs = RIGHT(@strIDs, DATALENGTH(@strIDs) - @sepPos)
        SET @sepPos = CHARINDEX(',', @strIDs)
      END

    SET @guess = CASE WHEN ISNULL(@guess, 0) <= 0 THEN 0.00001 ELSE @guess END

    SELECT @NPV = SUM(value / POWER(1 + @guess, id)) FROM @t_IDs
    WHILE @NPV > 0 
      BEGIN
        SET @guess = @guess + 0.00001
        SELECT @NPV = SUM(value / POWER(1 + @guess, id)) FROM @t_IDs
      END
    RETURN @guess
  END

Sample usage looks like this:

SELECT  dbo.ufn_IRR('-4000,1200,1410,1875,1050',0.00001)

But in my case I would like to avoid creating string from my columns, as this:

 SELECT  dbo.ufn_IRR(
    cast(round(sum([M0]), 2) AS NVARCHAR)+','
   +cast(round(sum([M1]), 2) AS NVARCHAR)+','
   +cast(round(sum([M2]), 2) AS NVARCHAR)+','
   +cast(round(sum([M3]), 2) AS NVARCHAR)+','
   +cast(round(sum([M4]), 2) AS NVARCHAR)+','
   +...+','
   +cast(round(sum([M20]), 2) AS NVARCHAR)
 ,0.00001)

I think it is pointless to first create a string and then inside function cut it into table.
I would like to modify IRR function so than I can pass my columns, but I don’t have Idea how I should do that :/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:11:58+00:00Added an answer on June 10, 2026 at 3:11 am
    CREATE FUNCTION [dbo].[ufn_IRR]
      (
       @s0 DECIMAL(30,10),
       @s1 DECIMAL(30,10),
       @s2 DECIMAL(30,10),
       @s3 DECIMAL(30,10),
       @s4 DECIMAL(30,10),
       @s5 DECIMAL(30,10),
       @s6 DECIMAL(30,10),
       @s7 DECIMAL(30,10),
       @s8 DECIMAL(30,10),
       @s9 DECIMAL(30,10),
       @s10 DECIMAL(30,10),
       @s11 DECIMAL(30,10),
       @s12 DECIMAL(30,10),
       @s13 DECIMAL(30,10),
       @s14 DECIMAL(30,10),
       @s15 DECIMAL(30,10),
       @s16 DECIMAL(30,10),
       @s17 DECIMAL(30,10),
       @s18 DECIMAL(30,10),
       @s19 DECIMAL(30,10),
       @s20 DECIMAL(30,10),
       @guess DECIMAL(30,10)
      )
    RETURNS DECIMAL(30, 10)
    AS 
      BEGIN
        DECLARE @t_IDs TABLE (
            id INT IDENTITY(0, 1),
            value DECIMAL(30, 10)
        )
        Declare @NPV DECIMAL(30, 10)
    
        INSERT INTO @t_IDs (value) values (@s0);
        INSERT INTO @t_IDs (value) values (@s1);
        INSERT INTO @t_IDs (value) values (@s2);
        INSERT INTO @t_IDs (value) values (@s3);
        INSERT INTO @t_IDs (value) values (@s4);
        INSERT INTO @t_IDs (value) values (@s5);
        INSERT INTO @t_IDs (value) values (@s6);
        INSERT INTO @t_IDs (value) values (@s7);
        INSERT INTO @t_IDs (value) values (@s8);
        INSERT INTO @t_IDs (value) values (@s9);
        INSERT INTO @t_IDs (value) values (@s10);
        INSERT INTO @t_IDs (value) values (@s11);
        INSERT INTO @t_IDs (value) values (@s12);
        INSERT INTO @t_IDs (value) values (@s13);
        INSERT INTO @t_IDs (value) values (@s14);
        INSERT INTO @t_IDs (value) values (@s15);
        INSERT INTO @t_IDs (value) values (@s16);
        INSERT INTO @t_IDs (value) values (@s17);
        INSERT INTO @t_IDs (value) values (@s18);
        INSERT INTO @t_IDs (value) values (@s19);
        INSERT INTO @t_IDs (value) values (@s20);
    
        SET @guess = CASE WHEN ISNULL(@guess, 0) <= 0 THEN 0.00001 ELSE @guess END
    
        SELECT @NPV = SUM(value / POWER(1 + @guess, id)) FROM @t_IDs
        WHILE @NPV > 0 
          BEGIN
            SET @guess = @guess + 0.00001
            SELECT @NPV = SUM(value / POWER(1 + @guess, id)) FROM @t_IDs
          END
        RETURN @guess
      END
    

    And use it

    dbo.ufn_IRR(round(sum([M0]), 2),
                round(sum([M1]), 2),
                .....
                round(sum([M20]),2),
                0.00001)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data that looks like this: Table Group: A , Color: Blue, Count:
I like to have a query like this: select data from table where (x
I have table data which looks like this id | keyword | count |
I have a data.table object like this one library(data.table) a <- structure(list(PERMNO = c(10006L,
I'm a bit confused on this. I have a data table structured like this:
I have a table like this: Application,Program,UsedObject It can have data like this: A,P1,ZZ
My table have data structure like this cate_id task_id date_start date_end other 34 14
I have a table like this... CustomerID DBColumnName Data 1 FirstName Joe 1 MiddleName
I have a table (call it table myTable). It has the data like this:
I have data in a table looking like this: +---+----+ | a | b

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.