同比和环比

def get_electric_num_then(self, obj):
    data = self.context

    start_date = datetime.datetime.strptime(data['start_date'], '%Y-%m-%d')

    last_date = start_date - relativedelta(years=1)

    end_date = datetime.datetime.strptime(data['end_date'], '%Y-%m-%d')

    
     This_year = \
     ElectricNum.objects.values('num').filter(create_time__gte=start_date, create_time__lte=end_date).aggregate(
         Sum('num'))['num__sum']
    
    last_year = \
    ElectricNum.objects.values('num').filter(create_time__gte=last_date, create_time__lte=start_date).aggregate(
        Sum('num'))['num__sum']

   
     end_year_num = This_year if This_year else 0
 
     start_year_num = last_year if last_year else 0

     if end_year_num == 0:
         end_year_num=ElectricNum.objects.filter(create_time__lte=end_date).order_by('update_time')
         end_year_num=end_year_num.last().num

   
     return start_year_num,end_year_num

def get_electric_num_Chain(self,obj):
    data=self.context
    start_date = datetime.datetime.strptime(data['start_date'], '%Y-%m-%d')

    last_date_month = start_date - relativedelta(months=1)

    end_date = datetime.datetime.strptime(data['end_date'], '%Y-%m-%d')

    end_date_month= end_date - relativedelta(months=1)

    result_month = \
        ElectricNum.objects.values('num').filter(create_time__gte=end_date_month, create_time__lte=end_date).aggregate(
            Sum('num'))['num__sum']

    last_month = \
        ElectricNum.objects.values('num').filter(create_time__gte=last_date_month, create_time__lte=start_date).aggregate(
            Sum('num'))['num__sum']

    start_date_num=last_month if last_month else 0
    end_date_num=result_month if result_month else 0



    return start_date_num,end_date_num


class Meta:
    model = Device
    fields = ('electric_num_then','electric_num_Chain')
### 同比环比是什么? **同比****环比**是统计学数据分析中常用的两个概念,用于衡量某个指标在不同时间段的变化趋势。它们广泛应用于经济、财务、运营分析等领域。 --- ## ✅ 一、同比(Year-on-Year, YoY) ### 🔹 定义: > **同比**是指当前周期的数据与**上年同期**数据进行比较。 ### 📌 公式: $$ \text{同比增长率} = \frac{\text{本期值} - \text{去年同期值}}{\text{去年同期值}} \times 100\% $$ ### 🎯 适用场景: - 比较今年6月 vs 去年6月 - 今年Q2 vs 去年Q2 - 适合有明显季节性波动的数据(如节假日销售、天气影响等) ### ✅ 优点: - 消除季节性影响(比如双11、春节效应) - 更能反映长期趋势 ### 💡 示例: > 今年7月销售额为120万元,去年7月为100万元 > 同比增长 = (120 - 100) / 100 × 100% = **+20%** --- ## ✅ 二、环比(Month-on-Month 或 Period-on-Period, MoM/PoP) ### 🔹 定义: > **环比**是指当前周期的数据与**上一个相邻周期**的数据进行比较。 ### 📌 公式: $$ \text{环比增长率} = \frac{\text{本期值} - \text{上期值}}{\text{上期值}} \times 100\% $$ ### 🎯 适用场景: - 本月 vs 上个月 - 本季度 vs 上一季度 - 连续时间序列的趋势分析 ### ✅ 优点: - 反映短期变化趋势 - 对业务波动更敏感 ### ❗注意: - 容易受季节性偶然因素干扰(例如:上个月是春节,这个月恢复正常) ### 💡 示例: > 今年7月销售额120万,6月为150万 > 环比变化 = (120 - 150) / 150 × 100% = **-20%** --- ## 🆚 对比总结表: | 维度 | 同比(YoY) | 环比(MoM) | |--------------|---------------------------------------|---------------------------------------| | 比较对象 | 上年同期 | 上一个周期(如上月) | | 目的 | 消除季节性,看长期趋势 | 观察短期波动 | | 举例 | 2024年8月 vs 2023年8月 | 2024年8月 vs 2024年7月 | | 是否受季节影响 | 小(相同季节对比) | 大(不同季节可能差异大) | | 常见用途 | 年报、季报、GDP增长、电商大促分析 | 日常监控、周报、月度绩效跟踪 | --- ## 🧩 实际应用中的组合使用 通常会**同时展示同比环比**来全面理解数据变化: ```text 某公司2024年9月营收: - 营收:1.2亿元 - 环比增长:+8% (比8月上升) - 同比增长:+25%(比去年9月上升) 👉 分析结论:不仅比上个月好,而且比去年同月大幅增长,说明业务持续向好。 ``` --- ## 🛠️ 在 SQL 中如何实现? 以 Oracle 为例,使用 `LAG()` 窗口函数计算: ```sql SELECT month, revenue, -- 环比:与上个月比较 LAG(revenue, 1) OVER (ORDER BY month) AS last_month_revenue, ROUND( (revenue - LAG(revenue, 1) OVER (ORDER BY month)) / LAG(revenue, 1) OVER (ORDER BY month), 4 ) AS mom_growth, -- 同比:与去年同月比较 LAG(revenue, 12) OVER (ORDER BY month) AS last_year_revenue, ROUND( (revenue - LAG(revenue, 12) OVER (ORDER BY month)) / LAG(revenue, 12) OVER (ORDER BY month), 4 ) AS yoy_growth FROM sales_data ORDER BY month; ``` > ⚠️ 假设 `month` 是连续的月度(如 '202301', '202302'...),每行代表一个月。 --- ## ✅ 总结一句话: - **同比看“跟去年比有没有进步”** → 长期趋势 + 抗季节干扰 - **环比看“跟上个月比是涨是跌”** → 短期动态 + 敏感反应 两者结合,才能看清“大局”与“细节”。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值