Here is the issue. I have looke through these threads for a while now and have yet to find the exact solution.
I have two tables like most question this has to do with joining the two tables. one is basicly a table with a list of runners little over 20,000 records currently the second table is a list of results for those runners it has alittle over 35000 records. The second table is actualy a view that contains all the result info plus age grades and a Rating which are generated by the view. What I need now is to combine the top three ratings per each runner. I tried this First.
SELECT
r.id,
r.FirstName,
r.MiddleName,
r.LastName,
r.Age,
r.Gender,
r.City,
r.State,
ROUND((SELECT SUM(re.Rating) rating FROM vwResults re WHERE re.runnerid = r.id ORDER BY re.Rating DESC LIMIT 3)/3,2) Rating
FROM Runners r
Unfortunitly that failed What I’m trying to do is sum the top three ratings from the results table and then devide it be 3 to get the avrage. this would be the avrage rating for that user. I have tried several diffrent variations of the code above and I did get one to work partialy but it took 8 seconds to load 30 records and the data was incorrect. Is there any way to do this so that I can create a new Runner view that has the rating built in? I would prefer not to use a procedure if at all possible.
Runners Table Structure
id int(11)
FirstName varchar(50)
MiddleName varchar(50)
LastName varchar(50)
Age int(3)
Gender varchar(2)
City varchar(50)
State varchar(2)
userid int(11)
vwResults View Structure
DistanceID int(4)
distance varchar(100)
RaceID int(11)
name varchar(100)
FirstName varchar(50)
LastName varchar(50)
place int(100)
Gender varchar(2)
age int(11)
finistime varchar(100)
pace time
agegrade varbinary(19)
ResultID int(11)
RunnerID int(11)
typeid int(11)
ftimesec bigint(10)
Rating decimal(18,2)
The only thing I needd from vwResults is the top three ratings sumed and / 3 to get a new avrage rating and all the info in the Runners table.
I am trying to create a function that will get the ratings but I know nothing about mysql functions could someone tell me what I am doing wrong here
CREATE FUNCTION GetRating(
Runnerid int( 11)
)
RETURNS decimal( 18, 2)
READS SQL DATA
BEGIN
DECLARE Rat decimal(18,2);
SELECT ROUND(SUM(Rating)/3,2) INTO Rat FROM vwResults WHERE runnerid = Runnerid GROUP BY runnerid
ORDER BY Rating DESC LIMIT 3;
RETURN(Rat);
END
I am trying to get the code below to work however there seems to be a problem with it keep getting the below error.
1064 – You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ” at line 3
CREATE PROCEDURE usb_GetRatings()
BEGIN
SET @rank = NULL;
SET @prevrunnerid = NULL;
DROP TABLE IF EXISTS ttRunners;
SELECT r.id, r.FirstName,r.MiddleName,
r.LastName,r.Age,r.Gender,r.City,r.State,
ROUND(top3rating.Rating, 2) as Rating INTO ttRunners
FROM Runners as r
JOIN (SELECT runnerid, AVG(Rating) as Rating
FROM (SELECT if(@prevrunnerid = runnerid,
@rank := @rank + 1,
@rank := 1) as rank,
@prevrunnerid := runnerid
as runnerid,
Rating
FROM vwResults
ORDER BY runnerid, Rating) as sq
WHERE rank <= 3
GROUP BY runnerid) as top3rating
on (r.id = top3rating.runnerid);
END
How about this? Put it in a procedure.