Jump to content

HELP.....Percentiles in mysql.....


clearstatcache

Recommended Posts

I was never hot at stats, but this will give each value and the percentage of records with values less than or equal to it. Is that what you mean? Avoid large data sets - this completely tied up my server for 7 mins for 10,917  records.

 

SELECT DISTINCT s.salesvalue,
(SELECT COUNT(*)/109.17 FROM sales b  WHERE b.salesvalue <= s.salesvalue) as pcent
FROM `sales` s
ORDER BY salesvalue

Link to comment
Share on other sites

This does the job in less than 1 second instead of the 7 mins taken by the previous query. Substitute your value column and table names

<?php

$sql = "SELECT COUNT(*) FROM sales";
$res = mysql_query($sql);
$total = mysql_result($res,0);

$sql = "SELECT salesvalue, COUNT(*) as cnt FROM sales
        GROUP BY salesvalue
        ORDER BY salesvalue";
$res = mysql_query($sql);
$cum = 0;
$pctiles =  array ();
while (list($v, $c) = mysql_fetch_row($res))
{
    $cum += $c;
    $pc = round($cum * 100 / $total);
    if ($pc % 25 == 0 && !isset($pctiles[$pc])) $pctiles[$pc] = $v;
}

echo '<pre>', print_r($pctiles, true), '</pre>';
?>

Link to comment
Share on other sites

  • 2 weeks later...

how would i do dat...please help...i need some guidance...

assuming i have a table like this..

 

+--------------+-------+-------------+

| Percentile_Key| Value  | Type        |

+----------------+-------+-------------+

|                  1 |      1 | Arrived    |

|                  2 |      2 | Arrived    |

|                  3 |      3 | Arrived    |

|                  4 |      3 | Arrived    |

|                  5 |      4 | Arrived    |

|                  6 |      5 | Arrived    |

|                  7 |      6 | Arrived    |

|                  8 |      7 | Arrived    |

|                  9 |      8 | Arrived    |

|                10 |      9 | Arrived    |

|                11 |    10 | Arrived    |

|                12 |    11 | Transferred |

|                13 |    12 | Transferred |

|                14 |    13 | Transferred |

|                15 |    14 | Transferred |

|                16 |    15 | Transferred |

|                17 |    16 | Transferred |

|                18 |    17 | Transferred |

|                19 |    18 | Transferred |

|                20 |    19 | Transferred |

|                21 |    20 | Transferred |

+--------------+-------+-----------+

 

i have a query :

 

select

    sum(Value),

    avg(Value)

from percentile

group by Type

order by Value

 

to get the sum and avg of the values by type...now i also want to get the 5th, 10,75th percentile of the values by type...

any idea how would i do that....

any help or idea is very much appreciated....

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.