【问题标题】:How to address numeric overflows in Amazon Redshift如何解决 Amazon Redshift 中的数字溢出问题
【发布时间】:2020-03-03 07:34:01
【问题描述】:

我在 Redshift 数据中有两个度量列:

两列的数据类型均为Double Precision,大小为53

colA              colB
590437.223579   350213.276421
142069.091151   76554.205749
119372.370247   23001.207853
Null                Null
0                    0
678.345678           0
  0             24567.567866

我想做一些这样的操作:

sum(colA/(colA+ colB)) over (partition by some_other_col) as agg

它让我除以零错误。

所以我做到了:

sum(cast(colA as decimal(3,2)) /nullif((cast(colA as decimal(3,2)) + cast (colB as decimal(3,2))),0)) over (partition by some_other_col) as agg       

它再次向我抛出错误:

          InternalError_: Numeric data overflow (scale float to decimal)
        DETAIL:  
-----------------------------------------------
     error:  Numeric data overflow (scale float to decimal)
     code:      1058
     context:   64 bit overflow
     query:     3941320
   location:  numeric_bound.cpp:72              

如何解决?

【问题讨论】:

    标签: sql amazon-web-services amazon-redshift


    【解决方案1】:

    您收到“除以零”错误,因为您没有处理分母为零。

    解决方案:添加nullif

    好吧,您这样做了,但是您还使用了“cast(...as decimal(3,2))”,这给了您“数字溢出”错误,因为您将原始值(大于 9.99)转换为十进制(3 ,2) 而不是铸造你的最终价值。

    如果您确实希望您的答案设置为小数 (3,2),您可以这样做。但请确保您的实际答案不大于 9.99。

    select cast(sum(colA /nullif((colA + colB),0)) over (partition by some_other_column) as decimal(3,2) ) as agg   
    from table; 
    

    注意:为简单起见,我假设所有行的“some_other_column”值相同。

    【讨论】:

      【解决方案2】:

      它让我除以零错误。

      修复你原来的问题:

      sum(colA/nullif(colA + colB, 0)) over (partition by some_other_col) as agg
      

      这将忽略 colA + colB 为 0 的行。

      显然,您的浮点数不适合 decimal(3,2)。这只支持从 0.00 到 9.99 的值。

      【讨论】:

      • @Mac 。 . .您无需强制转换即可解决除零问题。
      • 除以问题已经解决,但没有解决问题..nullif我得到InternalError_: Numeric data overflow (scale float to decimal) error
      猜你喜欢
      • 2011-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-12
      • 2021-03-05
      • 2019-07-02
      • 2016-06-27
      • 1970-01-01
      相关资源
      最近更新 更多