Jump to content

Getting row with highest value


Canman2005

Recommended Posts

Hi

 

I have a few questions, but i'll take it one at a time, firstly, have a table called `people` which looks like

 

 

   

name

   

score

 

 

   

David

   

150

 

 

   

Bryan

   

200

 

 

   

David

   

300

 

 

   

Susan

   

120

 

 

At the moment i'm using

 

SELECT MAX(score) FROM `people`

 

with that data it gives me 300 at the moment.

 

I want to do the same Query but count duplicates with the same `name`, so if I did this Query then it would return the value 450 as David has 2 entries and added together they eual the highest value.

 

Any help would be ace

 

Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/134635-getting-row-with-highest-value/
Share on other sites

Similar to the last question, but I have this QUERY

 

SELECT name, score*100/$max as pc FROM `people` ORDER BY score DESC

 

Which basically returns

 

David 300

Bryan 200

David 150

Susan 120

 

How can I do the same and count the total score for anyone with the same `name` so the result for that data would be

 

David 450

Bryan 200

Susan 120

 

But it would need to retain the part of the Query

 

score*100/$max as pc

 

as that is used later on in the code to work out an average score, but I need to try and group anyone with the same `name` together but adding up their `score`

 

sorry its a bit vague

 

any help would be wicked

 

thanks

Not sure if I follow:

SELECT name,
       SUM(score) AS totalScore,
       MAX(score) AS maximumScore,
       SUM(score)/MAX(score) * 100 AS pc
  FROM people
GROUP BY name
ORDER BY totalScore DESC

Does that help, even if it doesn't give exactly what you want

Just one question, where in the QUERY would I define the $max value?

 

if $max is already pre-defined (and you're sure it is a numeric value):

$query = 'SELECT name,
                 SUM(score) AS totalScore,
                 MAX(score) AS maximumScore,
                 SUM(score)/MAX(score) * 100 AS pc1,
                 100 * SUM(score)/'.$max.' AS pc
            FROM people
           GROUP BY name
           ORDER BY totalScore DESC';

hummm, that didnt seem to work either, so you know my code, I have the following

 

 

               

$maxres = mysql_query ("SELECT SUM(score) AS totalScore FROM people GROUP BY name ORDER BY totalScore DESC LIMIT 1");

 

Which works perfect, then I have the following

 

$max = mysql_result ($maxres,0);

                $sql = "SELECT name, score, score*100/$max as pc FROM people ORDER BY score DESC LIMIT 10";
                $res = mysql_query($sql);
                while (list($name, $score, $pc) = mysql_fetch_row($res))
                {
	printf('% (%0.1f<br>', $score, $pc/10);
	}

 

Which works and currently goes and returns everything in `score` order with a point score going from

 

0

0.1

0.2

.........

9.8

9.9

10.0

 

But what I want to do is to get it to group up anyone with the same `name` but adding their `scores` up and then returning a point score.

 

Does that help at all?

 

Thanks very much for all the help so far

[quote author=Canman2005 link=topic=227570.msg1050532#msg1050532 date=1227883235]
What does this give you?
[code=php:0]
$maxres = mysql_query ("SELECT SUM(score) AS totalScore FROM people");
$max = mysql_result ($maxres,0);
$sql = "SELECT name, SUM(score) AS totalScore, SUM(score) *100/$max as pc FROM people ORDER BY totalScore DESC LIMIT 10";
$res = mysql_query($sql);
while (list($name, $totalScore, $pc) = mysql_fetch_row($res)) {
   printf('%0.1',f$pc);
   echo ' - '.$name.' - '.$totalScore.'<br>';
}

 

mea culpa

$maxres = mysql_query ("SELECT SUM(score) AS totalScore FROM people");
$max = mysql_result ($maxres,0);
$sql = "SELECT name, SUM(score) AS totalScore, SUM(score) *100/$max as pc FROM people GROUP BY name ORDER BY totalScore DESC LIMIT 10";
$res = mysql_query($sql);
while (list($name, $totalScore, $pc) = mysql_fetch_row($res)) {
   printf('%0.1',f$pc);
   echo ' - '.$name.' - '.$totalScore.'<br>';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.