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
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

Link to comment
Share on other sites

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';

Link to comment
Share on other sites

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

Link to comment
Share on other sites

[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>';
}

 

Link to comment
Share on other sites

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>';
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.