Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating Percentage on the same column 1

Status
Not open for further replies.

Ant0

Technical User
May 3, 2005
17
GB
I have two tables, results and constits. In Constits I have area, in results I have party and votes. So results has, for each party the number of votes cast, and further to this an id col in results links it to constits.

I want to see:

SELECT constist.area, results.party, party.votes, (number_of_votes_for_party/total_number_of_votes_for_area)
FROM constits c
INNER JOIN results r
ON c.id = r.id

I am seriously struggling to work this out. I can return the votes per party and total votes per area separatly but I don't know to have both in one query so I can perform the maths.

Thanks
 
[tt]SELECT constist.area,
results.party,
party.votes,
(SELECT SUM(party_votes) FROM results
WHERE results.party = r.party)*1.0 /
(SELECT SUM(party_votes) FROM results)*1.0
FROM constits c
INNER JOIN results r
ON c.id = r.id[/tt]
 
Sorry about my answer above, wasn't meant to post it but pushed "submit" by accident (instead of "preview").
 
Is there really a column party.votes? (I can't find the party table...) Or do you mean party_votes, as a column in the results table? (That's what I've assumed.)

Well, my answer above does probably work (if I got you right).

But it can be somewhat simplified:
[tt]SELECT constist.area,
results.party,
party.votes,
party_votes*1.0 /
(SELECT SUM(party_votes) FROM results
WHERE results.id = r.id)*1.0
FROM constits c
INNER JOIN results r
ON c.id = r.id[/tt]
 
JarlH, thank you very much! This has helped me to solve my problem!!!

In answer to your question, that is my typo it should have been results.votes not party.votes. Either way your info helped me.

Now can I ask one more question..... I don't completely get it!!! I know this is terrible, but would you be able to explain it to me?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top