SQL Server : Multiply Row Values for the last month in a quarter

Rahul Polaboina 181 Reputation points
2022-01-05T18:42:53.46+00:00

Hello , I have a table with two columns Date and Monthly Return , I need to calculate a new column with a calculation, the formula is , for the last month in the quarter , the value should be the multiple of previous 2 months and the current month, for example : for march, the value should be (jan * feb * march ) and again for june the value should be (april * may * june), for the other months in the quarter the value should be as is.

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
SQL Server | Other
{count} votes

Answer accepted by question author
  1. Viorel 125.7K Reputation points
    2022-01-05T19:31:58.86+00:00

    Check a query:

    select *, 
        case when m % 3 = 0 then 
            isnull((select ReturnValue from ATable where year(EffectiveDate) = y and month(EffectiveDate) = m - 2), 1) *
            isnull((select ReturnValue from ATable where year(EffectiveDate) = y and month(EffectiveDate) = m - 1), 1) *
            ReturnValue
        else ReturnValue end as NewColumn
    from ATable
    cross apply (values (year(EffectiveDate), month(EffectiveDate))) M(y, m)
    

0 additional answers

Sort by: Most helpful

Your answer

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