Waffles
Waffles

Reputation: 103

How do I make this MySQL query work?

SELECT ROUND(AVG(((total - `order`) / total) * 100), 2) AS score 
FROM rankings
INNER JOIN ranking_data 
    ON ranking_id = rankings.id 
    AND opinion_id = {$opinion_id} 
    AND `type` = '{$type}'
INNER JOIN (
    SELECT rankings.id, COUNT(*) as total 
    FROM rankings
    INNER JOIN ranking_data 
        ON ranking_id = rankings.id AND `type` = '{$type}'
    WHERE case_id = {$case_id}
) AS total_table ON total_table.id = rankings.id
WHERE case_id = {$case_id};

MySQL Tables: http://pastebin.com/jKvUrrNi

Short Explanation:

The issue here is that there are two parts I need to perform this grading calculation. One row, and a total of related rows. The COUNT() function is messing up my selections. In the sub-query its not allowing me to select ON the current rankings.id (of the outer query) because the COUNT function reduces the results to one row before the filtering takes place. What I need is to somehow pass the id as a parameter in the sub-query's WHERE clause. This is only one idea to get the desired results, but there might be a better way if you understand the application (explained below).

Long Explanation:

Thie application allows users to rank opinions related to medical cases, for statistical purposes. There are two types of ranking which are computed the same way - pre-reference and post-reference. That reflects how the user chose to rank the opinions before they saw the reference (in the cases table) and afterward. opinion_list wer are not using at this time.

The rankings table stores one instance of a user going through the ranking process, and the ranking_data table stores the actual submitted data. The ranking page asks users to order opinions in order of preference, top being the best. This query is supposed to calculate the Collabograde score, which would be the amount people favored their opinion over others.

Users who enter the app first login, then submit their own opinion to the case, then rank their opinion against the others already submitted. Everyone does the same. For example, lets say I just submitted my opinion and started ranking. There are 4 opinions including my own in the system and no other rankings yet (although there would be one ranking per opinion typically). If I ranked my opinion at second best, then my Collabograde score would be 2/4. When user2 goes through the same ranking process and ranks my opinion at first best, then it becomes 3/8.

So for the query, order is 0 for best and so on. The query is supposed to calculate the Collabograde score for one user and one case. The opinion_id of the user's submission in the case is retrieved in a previous query. I first try to get the position of that opinion_id in all rankings, and then I get the total number of ranking_data rows for the currently selected rankings row. I use those values to calculate the result. So I am calculating one 'score' per rankings row, and then averaging them together to create the score for the entire case.

Geniuses far and wide, what can I do?

Upvotes: 1

Views: 190

Answers (1)

eggyal
eggyal

Reputation: 125835

You want to group the subquery by rankings.id, so add GROUP BY rankings.id:

SELECT ROUND(AVG(((total - `order`) / total) * 100), 2) AS score 
FROM rankings
INNER JOIN ranking_data 
    ON ranking_id = rankings.id 
    AND opinion_id = {$opinion_id} 
    AND `type` = '{$type}'
INNER JOIN (
    SELECT rankings.id, COUNT(*) as total 
    FROM rankings
    INNER JOIN ranking_data 
        ON ranking_id = rankings.id AND `type` = '{$type}'
    WHERE case_id = {$case_id}
    GROUP BY rankings.id
) AS total_table ON total_table.id = rankings.id
WHERE case_id = {$case_id};

Upvotes: 2

Related Questions