How do I query a table and get the ranking of records based off of multiple ranked records?

Posted on 2011-03-13
Last Modified: 2012-05-11
I have a table that has several records of ranks. The ranking can be 0-5.

So my table is like so...

record_ID |  project_ID  |  ranking  |  date |

1. record ID is just Auto Incremented
2. project_ID is a random number and can have multiple records of the same Project_ID
3. ranking can be 0-5 (0 = no points, 5 = Highest points).
4. And the date

I need for the query to COMBINE the records with the same project_ID and ORDER each combined record by the AVERAGE ranking (5 being first).

I've written some basic/intermediate SQL queries but this one is giving me some trouble.

Thank you,

Question by:brihol44
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 35125526
select avg(ranking), project_id from mytable group by project_id
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35125529
did you mean that you want one record for each project with average ranking ??

Project Id  ranking


Select Project_Id , Sum(ranking)/count(ranking) from Tablename
group by project_id

if not
give sampe data and show us what result you need

Expert Comment

ID: 35125535
sorry forgot the order by clause

select avg(ranking) as avg_rank, project_id from mytable group by project_id order by avg_rank desc
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.


Author Comment

ID: 35125676
Sorry but writing it out actually made it more clear for directions. I'll remember to do this in the future sorry if it's way off from what I was originally asking.


Accepted Solution

sihar86 earned 500 total points
ID: 35125781
SELECT project_ID, SUM(ranking), COUNT(1)*5
FROM tablename
WHERE ranking > 0
GROUP BY project_ID

Open in new window

since 0 would not count,
I add where ranking > 0
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35125783
try this
Select Project_Id , Sum(ranking) as with_point ,(count(Project_Id) *5 ) as out_of_point  from Tablename
group by project_id

Correction in your result
for project 3 have 2 records

so 5 out of 10

Expert Comment

ID: 35125887
SELECT project_ID, SUM(ranking), COUNT(project_ID)*5, date
FROM tablename
WHERE ranking > 0
GROUP BY project_ID

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Finding Where Clause Value in SQL Views and SP 21 60
WordPress  Failed to Import Media 8 64
SQL Query Works in SQL 2008 & 2012 But Not SQL 2016 15 98
Mysql query one to many 11 37
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question