Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Problem with SQL: combining a JOIN, SUM and MAX

I want to construct a query that provides me of the name of the party which received the most votes on their councilors.

The database is constructed in the following manner:
Party (PartyID, NameParty, StartDate, AmountofSeats)
Councilors (LidID, NameCouncilor, Council, Party, AmountofVotes)

Party in Councilors = PartyID in Party.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I wrote the following queries but both are not able to provide me with the correct party-name. Could someone help me out: what did I do wrong?

This code brings me the wrong party-name (it is not the highest amount of votes in total).

SELECT s.NameParty, s.SumVotes
    FROM (SELECT Party.NameParty, SUM(Councilors.AmountofVotes) SumVotes, Party.PartyID, Council.Party
        FROM Councilors
        JOIN Party on Party.PartyID = Councilors.Partij
        GROUP BY Party.PartyID) s
        WHERE s.SumVotes = (SELECT MAX(AmountofVotes)
                        FROM Councilors w1
                        WHERE w1.Party = s.PartyID);

Attempt2: This code gives me an error which has to do with the >= ALL but I do not understand why.

 SELECT s.NameParty, s.SumVotes
    FROM (SELECT Party.NameParty, SUM(Councilors.AmountofVotes) SumVotes, Party.PartyID, Councilors.Party
        FROM Councilors
        JOIN Partij on Party.PartyID = Councilors.Party
        GROUP BY Party.PartyID) s
        HAVING SUM(Councilors.AmountofVotes) >= ALL (
                SELECT SUM(Councilors.AmountofVotes) 
                FROM Councilors
                WHERE Councilors.Party = s.PartyID)

>Solution :

Now is a good time to learn about analytic functions. Use RANK() here:

WITH cte AS (
    SELECT p.PartyId, p.NameParty, SUM(c.AmountofVotes) SumVotes,
           RANK() OVER (ORDER BY SUM(c.AmountofVotes) DESC) rnk
    FROM Party p
    INNER JOIN Councilors c ON p.PartyID = c.Party
    GROUP BY p.PartyID, p.NameParty
)

SELECT PartyId, NameParty, SumVotes
FROM cte
WHERE rnk = 1;

Note that this approach is concise and is also robust to the possibility of ties between two or more parties having the most votes.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading