Help with SQL

Shyam Sreeramula 21 Reputation points
2024-05-11T17:16:52.7+00:00

Hi Experts,

Need help in creating calculated columns as below.

My Data:

User's image

Expected Result of two calculated columns:

User's image

Calculated Column 1 = Previous calculated value in Calculated Column 1 * Rate

Calculated Column 2 = RecentMinRent * current Value/Min(Value)

Appreciate your help.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,977 questions
SQL Server Transact-SQL
SQL Server Transact-SQL
SQL Server: A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.Transact-SQL: A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
101 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,652 questions
{count} votes

Accepted answer
  1. LiHongMSFT-4306 27,961 Reputation points
    2024-05-13T02:22:36.83+00:00

    Hi @Shyam Sreeramula

    Try this:

    SELECT *
    	  ,ROUND(RecentMinRent * EXP(SUM(LOG(rate)) OVER (ORDER BY ID ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)),0) AS Column1
    	  ,ROUND(RecentMinRent * VALUE/MIN(VALUE)OVER(ORDER BY ID),0) AS Column2 
    FROM YourTable
    

    Also, you could use recursive CTE to calculate column1, like this:

    ;WITH CTE AS
    (
     SELECT ID, VALUE, RecentMinRent, Rate, rate * RecentMinRent AS Column1 
     FROM YourTable
     WHERE id = 1
     UNION ALL
     SELECT y.ID, y.VALUE, y.RecentMinRent, y.Rate, x.Column1 * y.Rate
     FROM CTE x INNER JOIN YourTable y ON y.id = x.id + 1
    )
    SELECT ID, VALUE, RecentMinRent, Rate, Column1,ROUND(Column1,0)
    FROM CTE
    ORDER BY ID
    OPTION (MAXRECURSION MAX);
    

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Erland Sommarskog 112.7K Reputation points MVP
    2024-05-11T18:59:27.29+00:00

    So your first calculation column is a running product. Here is an example on you can achieve that:

    CREATE TABLE #rates (year   int   NOT NULL PRIMARY KEY,
                         rate   float NOT NULL)
    INSERT #rates(year,  rate)
       VALUES(2020, 1.01),
             (2021, 1.02),
             (2022, 1.03),
             (2023, 1.04),
             (2024, 1.05)
    SELECT year, rate,
           EXP(SUM(LOG(rate)) OVER (ORDER BY year ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW))
    FROM   #rates
    ORDER  BY year
    SELECT 1.01*1.02*1.03*1.04*1.05
    go
    DROP TABLE #rates
    

    The SUM with the OVER clause is a regular running sum, There is no product aggregate in SQL Server, but we work around this by using the mathematical formula:

    A*B = exp(log(A) + log(B))
    

    I'm afraid that I don't understand your second calculated column.

    Also, I like to point that it is better to post data as CREATE TABLE + INSERT statements, as this permits to copy and paste into a query window to work with the data. We can't copy and paste from images.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.