MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 17th, 2007, 10:56 AM
clem_c_rock clem_c_rock is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 18 clem_c_rock User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 39 m 56 sec
Reputation Power: 0
Calculation percentage on non-numeric columns

Hello,
trying to calculate the percentages of artist's from each country in our database.

I am drawing from 2 tables, the artists table which has a short country code and the citylatlong table which has the full country name.

Since the artists.country column is a varchar(2), is it possible to get the number of artists per country as a percentage.

Here's the query I'm trying:

SELECT artists.id, artists.country, countries.countryLONG, (SUM( artists.country ) / COUNT(artists.country )) *100 percentage FROM artists inner join (select countrySHORT, countryLONG from citylatlong GROUP BY countrySHORT, countryLONG) countries ON artists.country = countries.countrySHORT GROUP BY country ORDER BY country ASC limit 10 ;

The percentage column always comes out as 0.

Any ideas?

Thanks,
Clem c

Reply With Quote
  #2  
Old September 17th, 2007, 11:45 AM
clem_c_rock clem_c_rock is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 18 clem_c_rock User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 39 m 56 sec
Reputation Power: 0
found the solution - here it be:

SELECT c.country, 100.0 * COUNT(*) / ( SELECT COUNT(*) FROM artists ) AS percentage FROM artists
INNER JOIN ( SELECT countrySHORT , MAX(countryLONG) AS country FROM citylatlong GROUP BY countrySHORT) AS c ON c.countrySHORT = artists.country
GROUP BY c.country ORDER BY c.country ASC

Reply With Quote
  #3  
Old October 2nd, 2007, 06:35 PM
Arpee Ong Arpee Ong is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2007
Posts: 7 Arpee Ong User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 47 m 25 sec
Reputation Power: 0
Quote:
Originally Posted by clem_c_rock
Hello,
trying to calculate the percentages of artist's from each country in our database.

I am drawing from 2 tables, the artists table which has a short country code and the citylatlong table which has the full country name.

Since the artists.country column is a varchar(2), is it possible to get the number of artists per country as a percentage.

Here's the query I'm trying:

SELECT artists.id, artists.country, countries.countryLONG, (SUM( artists.country ) / COUNT(artists.country )) *100 percentage FROM artists inner join (select countrySHORT, countryLONG from citylatlong GROUP BY countrySHORT, countryLONG) countries ON artists.country = countries.countrySHORT GROUP BY country ORDER BY country ASC limit 10 ;

The percentage column always comes out as 0.

Any ideas?

Thanks,
Clem c


Hi Clem, have you tried casting artists.country column like this:

cast(artists.country as decimal)

in such way, it will be a valid divisor.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > Calculation percentage on non-numeric columns


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT