sparksql字段类型转换

1、spark sql 计算时,一定要注意精度的问题,一般像金额之类的值,要先转换为double或者 decimal来进行计算了。

一、sql的方式:
select
shop_id,order_id,sum(cast(deal_price as decimal(15,2))) deal_price,sum(cast(total_price as decimal(15,2))) total_price from aaa where stat_day='20190617' and cid1 not in (293,-1) and cid2 not in (422,421,427,525,446) and substr(pay_time,1,10)>='2019-06-01' and substr(pay_time,1,10)<='2019-06-17' group by shop_id,order_id
二、df的方式:
  c_tran_df.withColumn("l_picked_amt", c_tran_df("l_special_amt").cast(DecimalType(15,4)))
decimal(15,2)  
2 表示小数部分的位数,如果插入的值,未指定小数部分或者小数部分不足两位,则会自动补到2为小数。
15 表示整数部分加小数部分的总长度。         
原文地址:https://www.cnblogs.com/ssqq5200936/p/12041489.html