I am using MYSQL with PHP to do the server side scripting. I am having trouble trying to build a query that aggregates some review data that needs to be joined with some information about who the review is for.
I have two tables. One called "schools" with general information about a school and another called "reviews" with the user supplied review information.
The school table is structured as:
id, schoolName, schoolCity, schoolCountry (for the field names)
With example return data as:
1, ASD, Dubai, UAE
2, DAA, Dubai, UAE
etc.
The "reviews" table has 57 columns:
RID, schoolID, profileID, dateCreated, Q1, Q2, Q3,...Q51, Q52, freeReview (as field names)
with data returned like:
1, 8, 55, 2012-10-10 05:16:05, 10, 8, 9, 2, 3....
From Q1 to Q25, these are questions within a review form that are returned as integers between 0-10. The other columns tie to a profile table and the school table (as above).
I am able to create a query for an individual school that SUMS all of the reviews for the given school and generates an average score for the school based on the number of reviews submitted for that school.
What I am having trouble with is I would like to create a page that lists all (broken up by pagination) the schools in a table with that averaged and SUM-ed score.
This is the query I have that generates the overall average score for a school.
$singlereviewtotal = mysql_query("
SELECT ROUND(
((SUM(Q1+Q2+Q3+Q4+Q5+Q6+Q7+Q8+Q9+Q10+Q11+Q12+Q13+Q14+Q15+
Q16+Q17+Q18+Q19+Q20+Q21+Q22+Q23+Q24+Q25+Q26+Q27+Q28+Q29+Q30+
Q31+Q32+Q33+Q34+Q35+Q36+Q37+Q38+Q39+Q40+Q41+Q42+Q43+Q44+Q45+
Q46+Q47+Q48+Q49+Q50+Q51+Q52)/((52*10)*
(SELECT COUNT(*) FROM reviews WHERE schoolID='$schoolvalue')))*10), 2)
AS total
FROM reviews
WHERE schoolID='$schoolvalue'") or die(mysql_error());
This generates the correct summed and averaged value of all of the columns and rows, which is rounded to 2 decimals. The WHERE clause comes from a PHP value in the page that tells the query which school ID to use.
This is my attempt to try to get all of the schools into a list.
$sumreviews = mysql_query("SELECT *,
ROUND(
((Q1+Q2+Q3+Q4+Q5+Q6+Q7+Q8+Q9+Q10+Q11+Q12+Q13+Q14+Q15+
Q16+Q17+Q18+Q19+Q20+Q21+Q22+Q23+Q24+Q25+Q26+Q27+Q28+Q29+Q30+
Q31+Q32+Q33+Q34+Q35+Q36+Q37+Q38+Q39+Q40+Q41+Q42+Q43+Q44+Q45+
Q46+Q47+Q48+Q49+Q50+Q51+Q52)/(52*10)
*10),2) AS atotal
FROM reviews
RIGHT JOIN schools ON schools.SID = reviews.schoolID
GROUP BY schools.schoolName, reviews.schoolID
ORDER BY atotal DESC")or die(mysql_error());
This query gives me the list of schools, yet the output for "atotal" only gives me the SUM of the first row for that given school (which in this case is 7.31 - and can be seen just below in the output, but the averaged score should be 7.23).
If I remove the GROUP BY in the query above I get the list of all of the schools, plus extra rows for some schools who have more reviews - like American School of Dubai.
School Name, City, Country, Rating
American School of Dubai, Dubai, U.A.E. 7.31
Dubai American Academy, Dubai, U.A.E. 7.19
American School of Dubai, Dubai, U.A.E. 7.15
International School of Bangkok, Bankok, Thailand
American School of Rio de Janeiro, Rio, Brazil
American School of Budapest, Budapest, Hungary
Zurich International School, Zurich, Switzerland
I am wondering, how to construct the query so that the "AS atotal" rows for all the given schools are grouped and averaged? If I averaged the 7.31 with the 7.15 listed as individual review scores for the American School of Dubai, I would have the correct 7.23. The answer is just right there, but I can't seem to connect it together.
I apologize in advance for my lack of question "styling"!
Q1, Q2, Q3,...Q51, Q52Can you normalize this?