SQL-计算百分比增加?

时间:2018-08-21 16:53:23

标签: sql sql-server sql-server-2016

我的数据如下所示

ID  Name     Startdate     Fee     Increase     IncreaseDate 
23  PEN      01/08/2018    £65       Yes          01/12/2018 
33  PEL      02/06/2018    £100      No              NULL

我需要生成一个输出,显示每行的“费用”,并按年份细分(需要到2019年)

如果“增加”标志设置为“是”,则将从“增加日期”起将7%增加到“费用”列中。然后,在每个“增加日期”的周年纪念日,费用将增加7%。理想的输出如下所示,年份列为字符串格式。

ID  Name     Startdate     2018                          2019  
23  PEN      01/08/2018    £65, £69.55 from 01/12/2018   £69.55, £74.41 from 01/12/2019 
33  PEL      02/06/2018    £100                          £100  

谁能以最好的方式帮助实现这一目标?谢谢。

使用sql server 2016

杰西

2 个答案:

答案 0 :(得分:2)

这可能就是我要实现的目标:

设置(出于您的目的,CTE不是必需的,但用于我的测试):

DECLARE @SQL VARCHAR(MAX)
DECLARE @YearCounter INT = Year(GetDate())
DECLARE @FinalDate INT = Year(DateAdd(year, 3, GetDate()))

;WITH CTE (ID, Name, StartDate, Fee, Increase, IncreaseDate) AS (
    SELECT * FROM (
        VALUES
            ('23','PEN','1/8/2018',65,'Yes', '01/12/2018'), 
            ('33','PEL','2/6/2018',100,'No', Null)
    ) AS A (Column1, Column2, Column3, Column4, Column5, Column6)
)

查询:

SELECT * INTO #TEMP001
FROM CTE 

SELECT 
@SQL = 'SELECT ID,
    Name, 
    StartDate, 
    Fee as Orig_Fee, 
    IncreaseDate, '

;WITH years (years, interest) AS
   (SELECT @YearCounter AS years,
    CAST('(Fee*1.07)' AS VARCHAR(MAX)) AS interest
    UNION ALL
    SELECT years + 1 AS years, 
        interest + ' * ' + '(1.07)' AS interest
    FROM years
    WHERE years.years <= @FinalDate)

SELECT 
@SQL = @SQL + 
    'CASE 
        WHEN Increase = ''Yes'' THEN 
            CAST(ROUND(' + CONVERT(VARCHAR(MAX), interest) + ', 2, 1) AS DECIMAL(38, 2)) 
        ELSE Fee
    END AS [Fee_' + CONVERT(VARCHAR(4), years) + '], '
FROM years

SELECT
@SQL = LEFT(@SQL, LEN(@SQL)-1) + '
FROM #TEMP001 '

EXEC (@SQL)

DROP TABLE #TEMP001

需要注意的几件事:

  1. 我在查询中使用的ROUND会将多余的部分降到小数点后第二位,如果需要四舍五入,请将CAST(ROUND(' + CONVERT(VARCHAR(MAX), interest) + ', 2, 1) AS DECIMAL(38, 2))切换为CAST(ROUND(' + CONVERT(VARCHAR(MAX), interest) + ', 2) AS DECIMAL(38, 2))。但是根据您的结果,您似乎想删除它们。
  2. 您似乎(每年)尝试构建收益的方式异常,因此我将其划分为不同的列,从而避免了重复一些不必要的观点。 例如,您可以简单地合并这些列以创建您要从该产品中寻找的内容。
  3. 可以通过将DECLARE @FinalDate INT = Year(DateAdd(year, 3, GetDate()))的{​​{1}}行编辑为任意数字来更改未来年份。它将更改为未来(或2022年)的三年(+1)

预期输出:

3

除此之外,祝你好运。

答案 1 :(得分:1)

这是开始。它将取消从2019年到2022年的所有费用增加。它确实假设满足Increase = 'Yes'的任何记录将在所述范围内的每年增加。

    SELECT 
    INTO #temp 
            ID 
        ,   Name
        ,   Startdate 
        ,   Fee As [BaseFee]
        ,   to_char(IncreaseDate, 'mon-dd') as [IncreaseDate]
        ,   Fee * (1.07) AS [Fee2018] 
        ,   Fee * POWER(1.07,2) AS [Fee2019] 
        ,   Fee * POWER(1.07,3) AS [Fee2020] 
        ,   Fee * POWER(1.07,4) AS [Fee2021]
        ,   Fee * POWER(1.07,5) AS [Fee2022]
    FROM {table_name} AS t 
    WHERE t.Increase = 'Yes';


    SELECT 
            ID 
        ,   Name 
        ,   Startdate 
        ,   CAST(BaseFee AS VARCHAR) + ', ' + CAST([Fee2018] AS VARCHAR) + ' from ' + CAST(IncreaseDate AS VARCHAR) AS [2018]
        ,   CAST([Fee2018] AS VARCHAR) + ', ' + CAST([Fee2019] AS VARCHAR) + ' from ' + CAST(DATEADD(year, 1, IncreaseDate) AS VARCHAR) AS [2019]
        ,   CAST([Fee2019] AS VARCHAR) + ', ' + CAST([Fee2020] AS VARCHAR) + ' from ' + CAST(DATEADD(year, 2, IncreaseDate) AS VARCHAR) AS [2020]
        ,   CAST([Fee2020] AS VARCHAR) + ', ' + CAST([Fee2021] AS VARCHAR) + ' from ' + CAST(DATEADD(year, 3, IncreaseDate) AS VARCHAR) AS [2021]
        ,   CAST([Fee2021] AS VARCHAR) + ', ' + CAST([Fee2022] AS VARCHAR) + ' from ' + CAST(DATEADD(year, 4, IncreaseDate) AS VARCHAR) AS [2022]
    FROM #temp;
Drop #temp;

需要附加逻辑来检查[IncreaseDate],然后相应地调整年度输出。

2018年所有记录的费用都会增加吗?