PostgreSQL
 sql >> Base de Dados >  >> RDS >> PostgreSQL

Como consultar valores com a maior contagem de votos e sem sinalizadores no PostgreSQL?


Você pode tentar a consulta abaixo -
SELECT P.name, A.value, V.CNT_VOTES total_votes
  FROM properties P
  INNER JOIN assignments A ON P.id = A.property_id
  INNER JOIN (SELECT assignment_id, COUNT(*) CNT_VOTES
                FROM votes
               GROUP BY assignment_id) V ON V.assignment_id = A.id
  LEFT JOIN flags F ON F.assignment_id = A.id
                   AND F.assignment_id IS NULL
 ORDER BY V.CNT_VOTES DESC
 LIMIT 1;