Query help: avg on 2 columns for max values

Example: I have a table containing 2 columns called AVERAGE and AVERAGENETTO.

I want to have an AVG on my AVERAGE column, but in case on a row the AVERAGENETTO is larger then the AVERAGE field, then the AVERAGENETTO should be used.

How do I create such a query ?
Any help appreciated.

[php]
$result= mysql_query(“SELECT AVG(AVERAGE),AVG(AVERAGENETTO) FROM table_name”) or die(mysql_error());

$row = mysql_fetch_assoc($result) or die(mysql_error());

if (($row[‘AVERAGENETTO’]) > ($row[‘AVERAGE’]))
{
//use the AVERAGENETTO
}else
{
//use AVERAGE
}
[/php]

Thanks :slight_smile: I also experimented myself and came up with the following:

[php]$query = "SELECT gemiddelde, gemnetto FROM

";
$count=0;
$gemtotal=0;
$test = mysql_query($query);
while($row = mysql_fetch_array($test)) {
$gem = $row[‘gemiddelde’];
$gemn = $row[‘gemnetto’];
if ($gemn > $gem) {
$gemtotal=$gemtotal+$gemn;
$count=$count+1;
} else {
$gemtotal=$gemtotal+$gem;
$count=$count+1;
}
}
if ($count <> 0) {
$gemiddelderace = number_format($gemtotal / $count,1,’.’,’’);
} else {
$gemiddeldemtb = “0.0”;
}[/php]

looks good to me.

Good luck

Sponsor our Newsletter | Privacy Policy | Terms of Service