You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行sum(order_amount)/100.0遇Numeric data overflow错误,求助解决

数值溢出错误排查与解决方案

错误原因

该错误 Numeric data overflow (addition) 是由于执行 sum(order_amount) 时,累加后的总金额超出了 order_amount 字段的数据类型所能容纳的最大数值范围。此前未触发是因为累计金额未达到阈值,今日新增的数据(或某条超大金额订单)导致总和突破了限制。

排查步骤

  1. 确认字段数据类型
    执行以下查询查看 order_amount 的数据类型及精度:

    SELECT data_type, numeric_precision, numeric_scale 
    FROM information_schema.columns 
    WHERE table_schema = 'report_mongo' 
      AND table_name = 'ng_t_offline_merchant_acq_order' 
      AND column_name = 'order_amount';
    

    若返回的精度(如 numeric(18,0))不足以支撑当前累计总和,就是溢出的直接原因。

  2. 检查异常大额订单
    查询是否存在异常超大金额的订单:

    SELECT order_no, order_amount, update_time 
    FROM report_mongo.ng_t_offline_merchant_acq_order 
    WHERE child_trans_type IN ('71','73','74','76','e8')
      AND order_app_type=1 
      AND order_status=2 
    ORDER BY order_amount DESC 
    LIMIT 20;
    

    若存在远超正常范围的金额,可能是数据录入错误导致的。

解决方案

临时修复(无需修改表结构)

在求和前将字段转换为更大范围的数据类型,避免溢出:

  • 若字段为整数类型(如 int/smallint),转换为 bigint:
    select
        date(update_time) as dt,
        count(DISTINCT agent_id) as trans_users,
        count(order_no) as trans_cnt,
        sum(CAST(order_amount AS BIGINT))/100.0 as Money_amount
    from report_mongo.ng_t_offline_merchant_acq_order
    where child_trans_type in ('71','73','74','76','e8')
      and order_app_type=1
      and order_status=2
    group by 1
    order by date(update_time) desc 
    
  • 若字段为 numeric 类型,转换为更高精度的数值类型:
    sum(CAST(order_amount AS NUMERIC(38,0)))/100.0 as Money_amount
    

长期修复(修改表结构)

如果频繁出现溢出,建议直接修改字段类型为更大范围的类型:

ALTER TABLE report_mongo.ng_t_offline_merchant_acq_order 
ALTER COLUMN order_amount TYPE BIGINT; -- 或 NUMERIC(38,0),根据实际业务场景选择

注意:执行此操作需在业务低峰期进行,避免锁表影响正常业务。

内容的提问来源于stack exchange,提问作者lin youcehng

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 15:13:10