mysql - select average from average -


i have following table:

rating -------- | id | account_id | room | kitchen | bathroom | ----------------------------------------------- | 1  | 1          | 5    | 5       | 5        | | 2  | 1          | 2    | 4       | 1        | | 3  | 1          | 5    | 2       | 1        | ----------------------------------------------- 

people can rate room, kitchen , bathroom (from 1-5).

average rating id = 1: 5 (because 15/3 = 5)

average rating id = 2: 2.3333 (because 7/3 = 2.33333)

average rating id = 3: 2.6666 (because 8/3 = 2.66665)

first question

as can see, average rating id = 2 => 2.3333... , id = 3 => 2.6666. how can make floor() , ceil()? (when < .5 => floor, when > .5 => ceil), avg rating id = 2 becomes 2 (instead of 2.3333) , avg rating id = 3 becomes 3 (instead of 2.6666...)

second question

i want select average rating of average ratings (so average rating rows together). - when floor() , ceil() used have 3 average ratings: 5, 2 , 3 => 10/15 => 3. how 3?

thanks in advance!

for first question, answer round():

select round( (room + kitchen + bathroom) / 3) 

for second, use aggregation:

select avg(room + kitchen + bathroom) ratings; 

if want average of rounded results:

select round(avg(round(room + kitchen + bathroom))) ratings; 

however, seems strange me.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -