【Hive SQL 每日一题】环比增长率、环比增长率、复合增长率

文章目录

      • 环比增长率
      • 同比增长率
      • 复合增长率
      • 测试数据
      • 需求说明
      • 需求实现

环比增长率

环比增长率是指两个相邻时段之间某种指标的增长率。通常来说,环比增长率是比较两个连续时间段内某项数据的增长量大小的百分比。

环比增长率反映了两个相邻时间段内某种经济指标的变化速度,被广泛用于企业、社会和国民经济等方面的经济分析中。

环比增长率的计算公式如下:

环比增长率 = (本期数值 - 上期数值)/ 上期数值 * 100%

其中:

  • 本期数值是指当前时间段内的指标数值;

  • 上期数值是指上一个时间段内的指标数值。

通过计算两者之间的差异,再以百分比的形式表示出来,就得到了环比增长率。

例如,如果某公司今年第一季度的销售额为 100 万人民币,第二季度的销售额为 120 万人民币,那么环比增长率可以按照以下步骤计算:

环比增长率 = (120 - 100) / 100 * 100% = 20%

这样就得到了该指标在本期相对于上期的增长率为 20%

同比增长率

同比增长率是指与去年同期相比的增长率。它用于比较同一时间段内的两个不同年份的数据变化情况,判断增长趋势和比较不同年度的表现。常用于分析经济、市场等领域的年度变化趋势。

同比增长率的计算公式如下:

同比增长率 = (本期数值 - 去年同期数值)/ 去年同期数值 * 100%

其中:

  • 本期数值是指当前时间段内的指标数值;

  • 去年同期数值是指上一个年度同一时间段内的指标数值。

通过计算两者之间的差异,再以百分比的形式表示出来,就得到了同比增长率。

举例来说,如果某项指标在今年第一季度120,而去年同期第一季度100,那么同比增长率可以按照以下步骤计算:

同比增长率 = (120 - 100) / 100 * 100% = 20%

这样就得到了该指标在今年第一季度相对于去年同期的增长率为 20%

复合增长率

复合增长率是指在一段连续的时间内,某项指标每个月或年平均增长的复合增长率。它用于衡量某指标在一段时间内(月均或年均)的整体增长速度。

复合增长率的计算公式如下:

复合增长率 = (最终值 / 初始值)^( 1 / n) - 1

其中:

  • 最终值是指期末的数值;
  • 初始值是指起始的数值;
  • n 时间段数量是指经过 n 个时间段的增长所到达的值。

举例来说,假设某项指标在起始时刻(一月份)为 100,经过 6 个月(到达七月份)的增长,最终值为 200,则可以按照以下步骤计算月均复合增长率:

月均复合增长率 = ( (200 / 100)^(1 / (7-1)) -1) * 100%

这样就得到了某指标经过 6 个月的整体增长率(复合增长率) 为12.25%

在计算月均或年均复合增长率时,需要使用连续的起始值和结束值来进行计算。假设有 n 个连续的月份数据,那么起始值到结束值的时间跨度为 n-1 个月,只计算后续的增长情况。

测试数据

数据来源:Github

-- 创建销量表sales_monthly
-- product表示产品名称,ym表示年月,amount表示销售金额(元)
CREATE TABLE sales_monthly(product VARCHAR(20), ym VARCHAR(10), amount NUMERIC(10, 2));

-- 生成测试数据
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201801',10159.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201802',10211.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201803',10247.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201804',10376.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201805',10400.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201806',10565.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201807',10613.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201808',10696.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201809',10751.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201810',10842.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201811',10900.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201812',10972.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201901',11155.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201902',11202.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201903',11260.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201904',11341.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201905',11459.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('苹果','201906',11560.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201801',10138.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201802',10194.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201803',10328.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201804',10322.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201805',10481.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201806',10502.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201807',10589.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201808',10681.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201809',10798.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201810',10829.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201811',10913.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201812',11056.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201901',11161.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201902',11173.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201903',11288.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201904',11408.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201905',11469.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('香蕉','201906',11528.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201801',10154.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201802',10183.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201803',10245.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201804',10325.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201805',10465.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201806',10505.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201807',10578.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201808',10680.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201809',10788.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201810',10838.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201811',10942.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201812',10988.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201901',11099.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201902',11181.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201903',11302.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201904',11327.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201905',11423.00);
INSERT INTO sales_monthly (product,ym,amount) VALUES ('桔子','201906',11524.00);

需求说明

根据所给数据,分别计算下列三个指标:

  1. 计算各类商品的月环比增长率;

  2. 计算各类商品的月同比增长率;

  3. 计算各类商品的月均复合增长率。

需求实现

理解环比增长率、同比增长率、复合增长率的概念以及计算公式就能够比较容易的实现这个需求。

-- 月环比增长率
select
    product,
    ym,
    total_amount,
    lag_total_amount,
    if(
        lag_total_amount = 0,
        '0.00%',
        concat(cast((total_amount - lag_total_amount) / lag_total_amount * 100 as decimal (10,2)),'%')
    ) growth_rate
from
    (select
        product, ym, total_amount,
        lag(total_amount,1,0) over (partition by product order by ym) lag_total_amount
    from
        (select
            product,
            ym,
            sum(amount) total_amount
        from
            sales_monthly
        group by
            product,ym)t1 )t2;


-- 月同比增长率
select
    product,
    year,
    month,
    total_amount,
    lag_total_amount,
    if(
        lag_total_amount = 0,
        '0.00%',
        concat(cast( (total_amount - lag_total_amount) / lag_total_amount * 100 as decimal(10,2)),'%')
        )  year_growth
from
    (select
        product,
        substr(ym,1,4) year,
        substr(ym,-2) month,
        total_amount,
        lag(total_amount,1,0) over (partition by product,substr(ym,-2) order by substr(ym,1,4)) lag_total_amount
    from
        (select
            product,
            ym,
            sum(amount) total_amount
        from
            sales_monthly
        group by
            product, ym)t1 )t2
order by
    product,year,month;


-- 月均复合增长率
select
    product,
    ym,
    first_total_amount,
    total_amount,
    concat(cast(ifnull((pow(total_amount / first_total_amount, 1 / (rn-1) ) - 1)*100,0.00) as decimal(10,2)),'%') month_avg_compound_growth_rate
from
    (select
        product,
        ym,
        total_amount,
        first_value(total_amount) over (partition by product order by ym) first_total_amount,
        row_number() over (partition by product order by ym) rn
    from
        (select
            product,
            ym,
            sum(amount) total_amount
        from
            sales_monthly
        group by
            product, ym)t1 )t2;

你可能感兴趣的:(Hive,SQL,每日一题,hive,sql,大数据,数据仓库,数据库)