Calculation in Sql Server

前端 未结 6 675
孤街浪徒
孤街浪徒 2021-01-31 02:21

I trying to perform following calculation

Sample data:

CREATE TABLE #Table1
  (
     rno   int identity(1,1),
     ccp   varchar(50),
          


        
6条回答
  •  一生所求
    2021-01-31 03:14

    An approach with a self join. Not sure if this would be any more efficient than your version with cross apply.

    WITH T AS
      (SELECT *,
              ROW_NUMBER() OVER(PARTITION BY CCP
                                ORDER BY RNO) AS RN
       FROM #TABLE1)
    SELECT T1.RNO,
           T1.CCP,
           T1.COL1,
           T1.COL2,
           T1.COL3,
           T1.COL3-SUM(T2.COL1*POWER(1+T1.COL2/100.0,T1.RN-T2.RN+1)) AS RES
    FROM T T1
    JOIN T T2 ON T1.CCP=T2.CCP
    AND T1.RN>=T2.RN
    GROUP BY T1.RNO,
             T1.CCP,
             T1.COL1,
             T1.COL2,
             T1.COL3
    

    Sample Demo

提交回复
热议问题