简体   繁体   中英

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. Currently the averaged review score will come out as something like 7.40000000. Is there any way I can use both cast and sum together? Ideally I would like to cast it to a one point decimal figure, so that it would only display, say, 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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