* David Blomstrom
> Suppose you have a table with four columns. The first
> three colums each list a numeral, and the fourth
> column lists the sum of those numerals, like this:
>
> 3 | 3 | 4 | 10
> 10 | 10 | 5 | 25
>
> Now, suppose you wanted to also display those numerals
> as percentages:
>
> Reds | Blues | Grays | TOTAL
> 30% | 30% | 40% | 100%
> 40% | 40% | 20% | 100%
>
> (Actually, I may retain the original numerals in the
> last column...)
>
> 30% | 30% | 40% | 10
> 40% | 40% | 20% | 25

use test;
create table pct (red int,blue int,gray int);
insert into pct values (3,3,4),(10,10,5);
select @total:=red+blue+gray as total,
  concat(red/(@total/100),"%") as red,
  concat(blue/(@total/100),"%") as blue,
  concat(gray/(@total/100),"%") as gray
from pct;

Because of the use of a "user variable" (@total) to calculate the total in
my example, I have to have the total column first, because it must be
calculated before the individual percentage calculations. If you have a
total column in your table, you can use that in the calculations and place
it in the last column.

--
Roger


-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to