Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Using Max function

Explorer ,
Sep 22, 2008 Sep 22, 2008
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
TOPICS
Database access
402
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 22, 2008 Sep 22, 2008
LATEST
My approach would be to do 2 or 3 Q of Qs to get the numbers I need.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources