I have the following tables
Table Farm
+---------+--------+-------------------+-----------+------------+
| FARM_ID |Stock_ID| FARM_T
I would take your original query to get the second last table and change the Select
by adding use distinct
(found here) and only select Origin, Stock and the calculation for the Score. For example if the score is an average of all of them it would be AVG(Score)
where Score
would be what you fetched in the original query. If you want to use only a small subset of the items that have the same Origin and Stock to calculate the Score I would use a subquery, with the where matching the Origin and Stick ids, in the select so you have:
Select Origin,
Stock,
(select calculation(Score) from tables where tables.stock_id = .... tables.origin_id = .....)
From....
Hope this helps.