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

  • SEARCH
  • Home
  • 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 8529081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:56:57+00:00 2026-06-11T08:56:57+00:00

I have a table called ratings that looks like this Rating | Picid |

  • 0

I have a table called ratings that looks like this

Rating   |   Picid    |   User
   8           12          6
   7           12          6
   9           15          7
   5           16          7
   9           17          8
   10          2           8
   7           18          3
   10          22          12

I want to group the ratings based on the picid, but only choose the highest rated picid per user. I have the query like this already:

SELECT *, AVG(rating) AS total FROM ratings
  GROUP BY picid ORDER by total DESC

Right now the query would output the information like this:

Rating   |   Picid    |   User
 7.5           12          6
   9           15          7
   5           16          7
   9           17          8
   10          2           8
   7           18          3
   10          22          12

So it successfully groups the ratings based on the picid, but I want only the ONE highest per user. I want it to output information like this:

Rating   |   Picid    |   User
 7.5           12          6
   9           15          7
   10          2           8
   7           18          3
   10          22          12

See how it only allows the highest rated picid per user?
How can I modify my query to do this?

Let me know if you need more clarification on what I’m asking.

  • 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-11T08:56:58+00:00Added an answer on June 11, 2026 at 8:56 am

    You first must calculate the average ratings for each (Picid,User) combination, then find the group-wise maximum over that materialised result:

    SELECT Rating, PicID, User
    FROM (
      SELECT   AVG(Rating) AS Rating, Picid, User
      FROM     ratings
      GROUP BY Picid, User
    ) t1 NATURAL JOIN (
      SELECT   MAX(Rating) AS Rating, User
      FROM     (
        SELECT   AVG(Rating) AS Rating, User
        FROM     ratings
        GROUP BY Picid, User
      ) t
      GROUP BY User
    ) t2
    

    See it on sqlfiddle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table called 'MyTable' that looks like: ID | Item | Type
I have table called Direccion other called Cliente each one defined like this public
I have a mysql table called RATING with this structure: ID, USERNAME, RATING, RATER,
i have table called users , if i want to delete some user (User
i have table called table1 and it looks like the below record type tran_ref_number
i have table called type which contains data family,individual i want in some pages
Let's say I have a table like this (this is just a simplified example,
I have a table called ratings , and a column called item_id . I
I have table called location with a field area. This field can contain various
I have table called 'shipped_data' https://i.stack.imgur.com/JXBej.png and need go get all 'caseNumbers' that match

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.