Skip to main content
Inspiring
September 22, 2008
Question

Using Max function

  • September 22, 2008
  • 1 reply
  • 439 views
I have the following tables along with their attributes(the stared fields show the primary key:
Agent(AgentPhoneNo*, NameOfAgent)

Constituency(ConstituencyNo*, ConstituencyName,District,RegionCode)

PollingStation(StationNo*,ConstituencyNo*,AgentPhoneNo, StationName)

VoteResults(Party*, ResultsCategory*,Votes, AgentPhoneNo*)

Results category has two values (S,L) and the number of parties is variable

My issue is this, below is my code that returns the total votes received by each party with each constituency. This
query works fine.

<cfquery name="WinningPartyinConstituency" datasource="#request.datasource#">

SELECT c.ConstituencyName, s.ConstituencyNo, v.Party, Sum(v.Votes) AS TotalVotes
From (Constituency c
INNER JOIN PollingStation s ON c.ConstituencyNo = s.ConstituencyNo)
INNER JOIN VoteResults v ON s.AgentPhoneNo = v.AgentPhoneNo
WHERE v.ResultCategory = 'L' AND s.ConstituencyNo IN (Select ConstituencyNo From Constituency)
Group By c.ConstituencyName, s.ConstituencyNo, v.Party
Order By c.ConstituencyName

</cfquery>

Now I want to check and return the name and total votes of the party with the maximum votes within each constituency. And but I can't seem to figure out how to do that, especially because the max() function takes only to
values.

I will appreciate any guidance on how to write a query that returns the the party with the maximum votes in each constituency and the votes it got. TIA
This topic has been closed for replies.

1 reply

Inspiring
September 23, 2008
My approach would be to do 2 or 3 Q of Qs to get the numbers I need.