Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8307015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:26:42+00:00 2026-06-08T18:26:42+00:00

Here is the issue. I have looke through these threads for a while now

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:26:43+00:00Added an answer on June 8, 2026 at 6:26 pm

    How about this? Put it in a procedure.

    delimiter //
    
    CREATE PROCEDURE get_top3_rating_avg() AS BEGIN
        SET @rank = NULL;
        SET @prevrunnerid = NULL;
        SELECT r.id, r.FirstName,r.MiddleName,
               r.LastName,r.Age,r.Gender,r.City,r.State, 
               ROUND(top3rating.Rating, 2) as Rating 
        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 //
    
    delimiter ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've seen a million of these threads here already, and read through every single
Here's the issue: I have 2 data contexts that I would like to do
Here's the issue: I have a list of App names that I want to
Working with Reporting Services 2008 r2. So here's my issue: We have 5 reports
So I'm not to OOP in PHP. Here is my issue I have a
Having an issue here that I have tried everything I can think of but
I'm have a weird issue here. I have a UITableView using custom UITableViewCells. Everything
Here's a brief rundown of my issue: I have a JavaScript function that gets
I have a really weird issue here. I'm using my local development server right
I have a curious issue here In my ejbCreate() method from where i insert

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.