在 AWS Athena 中将小数转换为货币
Casting Decimal to Currency in AWS Athena
我有一个字段 value(decimal(23, 2))
,我正在这样查询它:
sum(value)
作为输出我得到 1200000.32
我正在尝试将其转换为货币以获得 .200.000,32
,如下所示:
cast(sum(value) as money) == FAIL
sum(value::money) == FAIL
cast(sum(gmv::numeric) as money) == FAIL
如何获得所需的货币类型?
There is no money type in presto. And Amazon's version of presto does not support format
and I could not find any buld in function to format numbers. So you will need to either create and use some 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})+,)', '.')
FROM dataset
输出:
_col0
,00
9,99
.000,10
1.000,10
.000.000,00
.001.100,10
我有一个字段 value(decimal(23, 2))
,我正在这样查询它:
sum(value)
作为输出我得到 1200000.32
我正在尝试将其转换为货币以获得 .200.000,32
,如下所示:
cast(sum(value) as money) == FAIL
sum(value::money) == FAIL
cast(sum(gmv::numeric) as money) == FAIL
如何获得所需的货币类型?
There is no money type in presto. And Amazon's version of presto does not support format
and I could not find any buld in function to format numbers. So you will need to either create and use some 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})+,)', '.')
FROM dataset
输出:
_col0 |
---|
,00 |
9,99 |
.000,10 |
1.000,10 |
.000.000,00 |
.001.100,10 |