Presto 除法/运算结果为0的原因及解决方法+cast用法
Presto 除法/运算结果为0的原因及解决方法
当我想统计一个数据比例使用/
结果为0,加号数据正常:
语法:
select sum(case when storecode = '15' then 1 else 0 end) / count(1) from orders;
但是同样的语句在hive中执行是ok的
在presto中:两个value相除,至少有一个为浮点数才能返回正确结果
select typeof(count(1)) from orders; 返回值:bigint
select 123 / 345 from orders limit 1;结果仍然为0
正确执行:****(用cast把其中一个转换为浮点型)
select sum(case when storecode = '15' then 1 else 0 end) / cast(count(1) as double) from orders;
我发现另一种好的用法
select sum(case when storecode = '15' then 1 else 0 end)*1.00 / count(1) from orders;
我们把分子乘1.00,结果就会自动保留两位小数,乘1.000就会保留3位(对于presto亲测有效)
cast 用法
presto的转换函数:cast
cast(value AS type)
显式转换一个值的类型如cast(1 as double)
将数字1
转为double
类型
SQL语句:
select * from table where date=20180101
在hive中正常执行,presto中会报错:operator equal(varchar, bigint) are not registered
原因:Presto不支持隐式转换,要求什么格式的参数,就一定得是什么格式的参数
改为select * from table where date=‘20180101’
还没有评论,来说两句吧...