首页 > 解决方案 > spark sql中以百万,千,单位为单位的拆分值

问题描述

如何在spark sql中以十亿,百万,千,单位和小数分割值

目前我尝试了以下选项

- ABS(数量)%1000000 输出:562191.7974663
单位- ABS(数量)%1000 输出:191.7974663
小数- ABS(数量)%1 输出:0.7974663 或 -0.56724

但是,我正在寻找下表中的输出。

Amount                   Amt_Billions    Amt_Millions   Amt_Thousands   Amt_Units  Amt_Decimals
3946562191.7974663       3000000000      946000000      562000          191        7974663
-743245613.56724                         743000000      245000          613        56724

请谁能帮我计算一下。

提前致谢。

标签: sqlapache-spark-sql

解决方案


嗯。. .

floor(amount / 1000000000) * 1000000000 as billions,
floor((amount % 1000000000) / 1000000) * 1000000 as millions,
floor((amount % 1000000) / 1000) * 1000) as thousands,
floor(amount % 1000) as units

小数是唯一棘手的。这些数字没有意义,因为 0.1、0.01、0.001 都将解析为1. 我建议在某些单位中这样做,例如百万:

floor((amount % 1) * 1000000) as units

推荐阅读