Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-13
7
Medium Priority
?
193 Views
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,

Brian
0
Comment
Question by:brihol44
[X]
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
7 Comments
 
LVL 5

Expert Comment

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

Expert Comment

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

like
Project Id  ranking

then

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
0
 
LVL 5

Expert Comment

by:mayankagarwal
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
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 

Author Comment

by:brihol44
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.

Brian
sc.jpg
0
 
LVL 2

Accepted Solution

by:
sihar86 earned 2000 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
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
0
 
LVL 5

Expert Comment

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

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

722 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