SQL-fu, .
// this would be the working query
SELECT
*,
@score := good / attempts * 100 AS score,
@t_score := (SELECT SUM(good / attempts * 100) FROM stats) as t_score ,
@score / @t_score as relative_score_good
FROM stats
, .
- , uncorrelated scalar subquery , , , ( EXPLAIN, , .
, ( !): user defined variables, @variable.
( , SQL ).
// create the demo table
CREATE TABLE `test`.`stats` (
`date` DATE NOT NULL ,
`id` INT NOT NULL ,
`attempts` INT NOT NULL ,
`good` INT NOT NULL ,
`bad` INT NOT NULL ,
INDEX ( `id` , `attempts` , `good` , `bad` )
) ENGINE = MYISAM
// inject some values
INSERT INTO `test`.`stats` (`date`,`id`,`attempts` ,`good` ,`bad`)
VALUES
('2010-08-23', '1', '5', '4', '1'),
('2010-08-23', '2', '10', '6', '4'),
('2010-08-23', '3', '6', '3', '3'),
('2010-08-23', '4', '8', '2', '6');
, ! , , - . 4 , !;)