php - How do I Get the cummulative total of equation in MySql statement -
i have following mysql statement, cannot cummulative total 'ranking' in while loop
select *, q1 + ((q1 + q2 + q3 + q4 + q5 + q6) / 6) ranking / sum(ranking) table field = :field group id
this part of query works perfectly:
q1 + ((q1 + q2 + q3 + q4 + q5 + q6) / 6) ranking
i need divide 'as ranking' value sum of each 'ranking' row in query output , return in $rankingpercentage variable in while loop.
q1
q2
q3
q4
q5
q6
fields in 1 table , contain number values (varchar) between 0 , 100.
i expect return output $rankingpercentage 'ranking' (my equation above "as ranking") divided sum of 'ranking' rows returned in sql query.
my while loop below:
while($row = $sqlprep->fetch(pdo::fetch_obj)){ $ranking = $row->ranking; echo '<tr valign="bottom"><td>' . $row->id . '</td>'; echo '<td>' . $row->uid . '</td>'; echo '<td>' . $row->q1 . '</td>'; echo '<td>' . $row->q2 . '</td>'; echo '<td>' . $row->q3 . '</td>'; echo '<td>' . $row->q4 . '</td>'; echo '<td>' . $row->q5 . '</td>'; echo '<td>' . $row->q6 . '</td>'; echo '<td>' . $row->q9 . '</td>'; echo '<td class="capitalise">' . $ranking. '</td>'; echo '<td class="capitalise">' . $rankingpercentage . '</td>'; echo '</tr>'; }
as understand it, have formula assign score per row, , need see portion of total score across rows row earns.
a sub-select solve this, though join more efficient.
select *, <formula> ranking <formula> / ( select sum(<formula>) table field = :field ) rankingpercentage table field = :field group id
this tell part of whole each row owns, not tell given row in top x%.
Comments
Post a Comment