PostgreSQL
 sql >> Database >  >> RDS >> PostgreSQL

Come eseguire una query per i valori che hanno il conteggio dei voti più alto e nessun flag in PostgreSQL?

Puoi provare la query seguente -

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;