Please, can you review this query? I can't retrieve the correct values with the sub-query. I believe my logic is incorrect.
I'm trying to make this work day after day, but without success.
Above the SQL query in the SQLfiddle has the comments to reproduce the error.
With this sketch you can have a better understanding of what I need.
SELECT P.nome, centroide_produto_id, similar_produto_id,
(SELECT preco_venda FROM precos A
WHERE A.produto_id = CL.similar_produto_id
AND A.preco_id = (SELECT Max(preco_id)
FROM precos A2,
cluster_copy3 CL2
WHERE A2.produto_id = A.produto_id
AND A2.produto_id=CL2.similar_produto_id
AND A2.preco_venda = (SELECT Max(preco_venda)
FROM precos A3,
cluster_copy3 CL3
WHERE A3.produto_id = CL3.similar_produto_id
AND CL3.centroide_produto_id = CL.centroide_produto_id))) AS preco_maximo
FROM produtos P,
cluster_copy3 CL
WHERE P.produto_id = CL.centroide_produto_id
AND CL.centroide_produto_id IN (9817, 9816)
#GROUP BY CL.centroide_produto_id