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

Trova la proporzione di ogni X composta da Y in PostgreSQL?

Risolto!

SELECT
    d.id,
    c.cmc,
    (CAST(SUM(m.count) AS FLOAT) / 
        (SELECT
            CAST(SUM(m1.count) AS FLOAT)
        FROM deckmembers AS m1
        JOIN cards AS c1 ON c1.id=m1.card
        WHERE NOT m1.sideboard
        AND c1.fulltype NOT LIKE '%Land%'
        AND m1.deck=d.id)
    ) * 100 AS perc
FROM deckmembers AS m
JOIN decks AS d ON d.id=m.deck
JOIN cards AS c ON c.id=m.card
WHERE NOT m.sideboard
AND c.fulltype NOT LIKE '%Land%'
GROUP BY d.id, c.cmc;

Ho anche pubblicato una soluzione alla versione più semplice del problema qui .