通过基于前一行计算来更新列

时间:2013-12-18 15:27:58

标签: sql sql-server

我有以下三个表。 首先,我使用的是SQL Server 2005。

我想在这里明确指出以下3个表

  1.Table Name:  PrimaryData   (Primary Key : SNo)

  SNO   Name, Designation BPS GrossPension

   1   ABCDEF  zzzzzz     21    25000
   2   WXYZA   YYYYYY     19    20000   

On basis of GrossPension , i have to calculate NetPension for each year


 **2. Table Name IncrementYear  (Primary Key : ID)**   
Following table shows the number of years with increase percentage value for each year, we have to calculate NetPension according to this table.
ID   IncYear Percentage

1   2010    15
2   2011    20
3   2012    15
4   2013    15
5   2014    15
6   2015    15
7   2016    15
8   2017    15
9   2018    15
10  2019    15
11  2020    12

通过存储过程,我将值从上面的两个表中插入下表中i-e PrimaryData和IncrementYear

3. Table Name: PensionTable (Composite Primary Key : SNO ,Year)

由列组成:  enter image description here

计算NetPension的公式是

  NetPension =   (GrossPension * 15 )/100   

,其中15是2010年(年)表增量年份的百分比

注意:此处仅提及2010年,我必须从GrossPension(来自PrimaryData)计算netPension 2010.

  • 在计算2010年的NetPension后,使用NetPension 2010,我必须计算接下来的一年NetPension i-e 2011,2012 .... 2020

  • 计算年度2011年NetPension公式为:(2010年NetPension * 20)/ 100 + 2010年NetPension,其中20为2011年表增量年份的百分比。

  • 计算2012年NetPension公式的
  • 将是:

    (2011 NetPension * 15)/100 + 2011 NetPension

,其中15是2012年表增量年份的百分比。

依旧......

这里的NetPension列基于每个上一年的NetPension,在这方面帮我,如果还不清楚请发给我你的电子邮件我会发给你完整的数据库文件。


i-e 


  2010: (25000 * 0.15)+25000 =28750
    2011:  (28750* 0.2)+28750 = 34500
    2012:  (34500 * .15)+34500 = 39675

2 个答案:

答案 0 :(得分:0)

我很确定这个查询是正确的。它递归计算IncrementYear表中每条记录的净养老金。可能存在一些舍入问题,因此我建议您完全验证金额:

;WITH CompoundRates
AS
(
    SELECT
        Sno,
        Bps,
        PrimaryData.GrossPension + (PrimaryData.GrossPension * IncrementYear.Percentage / 100) NetPension,
        IncrementYear.ID
    FROM
        PrimaryData 
        INNER JOIN IncrementYear ON IncrementYear.ID = 1
    WHERE
        ID = 1
    UNION ALL
    SELECT
        Compoundrates.Sno,
        Compoundrates.Bps,
        CompoundRates.NetPension + (CompoundRates.NetPension * IncrementYear.Percentage / 100),
        IncrementYear.ID
    FROM
        IncrementYear
        INNER JOIN CompoundRates ON CompoundRates.Id + 1 = IncrementYear.ID
)
SELECT * FROM CompoundRates ORDER BY Sno, ID

答案 1 :(得分:0)

> I did update PensionTable with following code

;WITH CompoundRates
    AS
    (
        SELECT
            Sno,
            Bps,
            PrimaryData.GrossPension + (PrimaryData.GrossPension * IncrementYear.Percentage / 100) NetPension,
            IncrementYear.ID,IncrementYear.[IncYear]
        FROM
            PrimaryData 
            INNER JOIN IncrementYear ON IncrementYear.ID = 1
        WHERE
            ID = 1
        UNION ALL
        SELECT
            Compoundrates.Sno,
            Compoundrates.Bps,
            CompoundRates.NetPension + (CompoundRates.NetPension * IncrementYear.Percentage / 100),
            IncrementYear.ID,IncrementYear.[IncYear]
        FROM
            IncrementYear
            INNER JOIN CompoundRates ON CompoundRates.Id + 1 = IncrementYear.ID
    )
    --SELECT * FROM CompoundRates ORDER BY Sno, ID
    Update PT Set PT.NetPension = CR.NetPension from PensionTable PT 
    Inner Join CompoundRates CR on  PT.[YEAR]=CR.[IncYEAR] and PT.BPS=CR.BPS