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

Encontre a proporção de cada X consistindo de Y no PostgreSQL?


Resolvi-o!
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;

Também postei uma solução para a versão mais simples do problema aqui .