PostgreSQL
 sql >> Datenbank >  >> RDS >> PostgreSQL

Finden Sie den Anteil jedes X, das aus Y besteht, in PostgreSQL?

Gelöst!

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;

Ich habe auch eine Lösung für die einfachere Version des Problems gepostet hier .