简体   繁体   English

SQL:同时使用 SUM 和 CAST

[英]SQL: Using Both SUM and CAST

Below is a query to show columns from a Review table, and to average out subratings into a master rating.下面是一个查询,用于显示 Review 表中的列,并将子评分平均为主评分。 Currently the averaged review score will come out as something like 7.40000000.目前,平均评论分数将类似于 7.40000000。 Is there any way I can use both cast and sum together?有什么办法可以同时使用 cast 和 sum 吗? Ideally I would like to cast it to a one point decimal figure, so that it would only display, say, 7.4.理想情况下,我想将其转换为一位小数,这样它只会显示 7.4。 I've tried to edit my query based on other answers I've seen but I can't seem to get it to work.我试图根据我看到的其他答案来编辑我的查询,但我似乎无法让它工作。

SELECT Review_ID, Customer_ID, Property, Room, Order_ID, 
        Date_of_Stay,Customer_Country, 
        SUM((Rating_Cleanliness+Rating_Comfort+Rating_Location+Rating_Staff+Rating_Value)/5) AS Review_Score
FROM `PCT_Review`
GROUP BY Review_ID;

Many thanks.非常感谢。

I think you can use them together我想你可以一起使用它们

SELECT Review_ID, 
       Customer_ID, 
       Property, 
       Room, 
       Order_ID, 
       Date_of_Stay,
       Customer_Country,cast(SUM((Rating_Cleanliness+Rating_Comfort+Rating_Location+Rating_Staff+Rating_Value)/5)  as decimal (10,1)) 
       AS Review_Score
FROM `PCT_Review`
GROUP BY Review_ID, 
         Customer_ID, 
         Property, 
         Room, 
         Order_ID, 
         Date_of_Stay,
         Customer_Country;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM