I have these tables in a database:
Agent(AgentPhone*, AgentName)
Constituency(ConstituencyNo*,ConstituencyName, RegionCode, ConstituencyDistrict)
Party(PartyCode*, PartyName)
PollingStation(StationNo*,ConstituencyNo*,RegionCo de, AgentPhone, StationName)
Region(RegionCode, RegionName), and
VoteResults(Party*, ResultsCategory*, Votes, AgentPhone)
* Represents the fields that form the primary keys. Results Category are L and S (representing Parliamentary and Presidential results respectively).
My question is, is it possible to write a single query to return the following:
The constituency name, its region, sum of votes of a particular category for each party? I have tried variousl combinations and cannot get a query that does this.
Finally is a sub query contains an aggregate function with an alias, how do you call that result in the calling query? For example I want to show each constituency and the votes of a particular category accruing to it and I am using the aggregate SUM(Votes) AS TotalVotes. How do I refer to this result in the calling query?
Thanks for the help. Sorry is my question is too cumbersome.
ps: I am using this within ColdFusion