presto sql - 两列的聚合乘积,数字存储为字符串

presto sql - aggregate product of two columns with numbers stored as strings

我正在尝试聚合两列的乘积,其中数字存储为字符串。我试图将列转换为数值,然后相乘并聚合,但出现错误。


查询是在 Amazon/Athena 环境中构建的 (Presto)


更新
经过进一步调查,我发现在数据集中有一些价格为负的记录,这些记录很可能是这里出现问题的原因


有例子:

"dataset"."table1"

product   price    quantity
==========================
(string) (string)  (string)
 A        5         1 
 A       -1        1
...

SQL代码

 SELECT 
    product
    , sum ( coalesce(cast(nullif(price,'') as DECIMAL(28, 2)),0) * coalesce(cast(nullif(quantity,'') as DECIMAL(28, 2)),0))
 FROM "dataset"."table1" 
 WHERE
    price is not NULL and price not like '0' 
    and quantity is not NULL  and  quantity not like '0' 
    GROUP BY
    product

ERROR: INVALID_CAST_ARGUMENT: Cannot cast VARCHAR ' SUBS' to DECIMAL(28, 2)

虽然下面的查询工作正常

SELECT 
product
,coalesce(cast(nullif(price,'') as DECIMAL(28, 2)),0)
,coalesce(cast(nullif(quantity,'') as DECIMAL(28, 2)),0)
from "dataset"."table1" 
WHERE
price is not NULL and price not like '0' 
and quantity is not NULL  and  quantity not like '0' 

如何绕过转换并汇总两列的乘积?

我会考虑将您的 CAST 包装在 TRY 中(参见 https://prestodb.io/docs/current/functions/conditional.html)。 您在上面显示的错误表明您的数据不干净。 无法将字符串“SUBS”转换为数字。

最后我解决了在 SELECT

中使用 WHEN CASE 的问题
sum( CAST( (CASE WHEN price like '-%' Then  concat('-', substr(price, 2, Length(price)) )  ElSE price  END ) as DECIMAL(28,2) ) * CAST( quantity as DECIMAL(28,2) ) )