SQL使用Case转换数据类型

时间:2017-03-02 00:40:41

标签: sql case type-conversion

我试图将所有付款的总金额相加,当我这样做时,答案是用科学记数法给出的,而不是具有特定小数位数的浮点数。我关注的专栏是total_amount,如果你能告诉我这样做的正确方法以及Case功能的一些背景,那将是很棒的。

Select customer.customer_id, customer.email, count(payment.payment_date) as payments_count,
round(sum(Cast(payment.amount as numeric(4,2))), 2)  as total_amount 
From customer join payment 
on customer.customer_id = payment.customer_id
group by customer.customer_id

1 个答案:

答案 0 :(得分:0)

round()不会更改数据类型,只会更改数值。相反,在执行sum()之后

Select c.customer_id, c.email, count(p.payment_date) as payments_count,
       Cast(sum(p.amount) as numeric(4, 2))  as total_amount 
From customer c join
     payment p
     on c.customer_id = p.customer_id
group by c.customer_id;