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
187 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
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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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 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
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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