【问题标题】:Casting Decimal to Currency in AWS Athena在 AWS Athena 中将小数转换为货币
【发布时间】:2021-11-13 13:47:26
【问题描述】:

我有一个字段value(decimal(23, 2)),我正在像这样查询它:

sum(value) 作为输出我得到1200000.32

我正在尝试将其转换为货币以获取$1.200.000,32,如下所示:

cast(sum(value) as money) == FAIL
sum(value::money) == FAIL
cast(sum(gmv::numeric) as money) == FAIL

如何获得所需的货币类型?

【问题讨论】:

    标签: sql amazon-athena presto


    【解决方案1】:

    There is no money type 很快。而且亚马逊的 presto 版本不支持format,我找不到任何格式化数字的函数。因此,您需要创建并使用一些 user defined function 来执行此操作,或者使用一些替换和正则表达式魔术:

    WITH dataset(d) AS (
       VALUES 
      (decimal '1'),
      (decimal '999.99'),
      (decimal '1000.1'),
      (decimal '101000.1'),
      (decimal '1000000'),
      (decimal '10001100.10')
     ) 
     
    SELECT '$' || regexp_replace(replace(cast(d as VARCHAR), '.', ','), '(\d)(?=(\d{3})+,)', '$1.')
    FROM dataset
    

    输出:

    _col0
    $1,00
    $999,99
    $1.000,10
    $101.000,10
    $1.000.000,00
    $10.001.100,10

    【讨论】:

    • 感谢@Guru Stron
    • @pouchewar 很乐意提供帮助!
    猜你喜欢
    • 1970-01-01
    • 2022-12-03
    • 2017-01-06
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多