php - How do I get the average from a mysql column minus highest and lowest value -
i'm stumped. want average "clicks" "posts" throw away highest value typically high skews threshold when want display posts clicks above average. want discard lowest value same reason. code is:
//get sum of clicks posts $gettotalsum = mysql_query("select sum(clicks) click_total posts clicks > '0' "); $clicksum = @mysql_fetch_array($gettotalsum); //get number of posts have clicks $postcount = mysql_query("select postid posts isactive = 'y' , clicks > '0' "); $clickcount = mysql_num_rows($postcount);
i suspect 2 queries above need modify filter highest , lowest click values - how?
and here's rest of code fwiw:
//multiply average, round off $average = $clicksum['clicks'] / $clickcount; $average = round($average); //display posts equal or above average $getposts = mysql_query("select postid, posturl, posttitle, clicks posts clicks >= $average order clicks desc"); while ($aboveaverageposts = @mysql_fetch_array($getposts)) { //code format output goes here }
i suspect code can streamlined, important right i'm looking filter highest , lowest values.
thanks in advance.
to average minus highest , lowest values (as title says) bit tricky. want work:
select (sum(clicks) - max(clicks) - min(clicks)) / (count(*) - 2) avgclicks posts clicks > '0'
unfortunately, have duplicate records largest , smallest values, count woud off. approach should work:
select avg(clicks) avgclicks posts p cross join (select max(clicks) maxclicks, min(clicks) minclicks posts p clicks > 0 ) minmax clicks > minmax.minclicks , clicks < minmax.maxclicks;
to "above average" clicks, can put in where
clause:
select p.* posts p clicks > (select avg(clicks) avgclicks posts p cross join (select max(clicks) maxclicks, min(clicks) minclicks posts clicks > 0 ) minmax clicks > minmax.minclicks , clicks < minmax.maxclicks )
Comments
Post a Comment