RavenBlog
Black:  ravenblack.net | me | games | books | email | wishlist | rss
Blogs:  Angryblog | As Above | MonkyBlog | Nightshade | Journals
Blimey:  SomethingAwful | Advice
Archives: Last 4 Days | June2001 | July2001 | August2001 | September2001 | October2001 | November2001 | December2001 | January2002 | February2002 | March2002 | April2002 | May2002 | June2002 | July2002 | August2002 | September2002 | October2002 | November2002 | December2002 | January2003 | February2003 | March2003 | April2003 | May2003 | June2003 | July2003 | August2003 | September2003 | October2003 | November2003 | December2003 | January2004 | February2004 | March2004 | April2004 | May2004 | June2004 | July2004 | August2004 | September2004 | October2004 | November2004 | December2004 | January2005 | February2005 | March2005 | April2005 | May2005 | June2005 | July2005 | August2005 | September2005 | October2005 | November2005 | January2006 | February2006 | March2006 | April2006 | May2006 | June2006 | July2006 | August2006 | September2006 | October2006 | November2006 | December2006 | January2007 | February2007 | March2007 | April2007 | May2007 | June2007 | July2007 | August2007 | September2007 | October2007 | November2007 | December2007 | January2008 | February2008 | March2008 | April2008 | May2008 | June2008 | July2008 | August2008 | September2008 | October2008 | November2008 | December2008 | January2009 | March2009 | April2009 | May2009 | July2009 | August2009 | September2009 | February2010 | March2010 | June2010 | July2010 | August2010 | September2010 | October2010 | November2010 | December2010 | February2011 | March2011 | April2011 | May2011 | June2011 | July2011 | August2011 | September2011 | October2011 | December2011 | March2012 | April2012 | May2012 | September2012 | December2012 | March2013 | April2013 | May2013 | June2013 | October2021


Comments on Sunday 7 February 2010:
As of a couple of weeks ago, Jessica and I are engaged.

In other news that someone else might care about and that I might want to refer back to at some point, I found a reasonably quick way of putting an approximate percentile into a big SQL table (in mysql at least). The solution is:
SET @count=(SELECT COUNT(*) FROM table);
SET @row=0;
UPDATE table SET percentile=(@row:=@row+1)/@count ORDER BY whatever;
That should be *100 for an actual percentile, but I prefer to work with the real number. If you want the top one of 5 values to be 80th percentile rather than 100th then row should start at -1 rather than at 0, and similarly if you want the percentiles to never be 0 or 100 then @row starts at 0 and @count should be count(*)+1.

Main flaw in this model is that tied values don't get the same percentile, but rather a distribution of percentiles through the number of ties, ordered by some indeterminate secondary criterion (or a determinate one if you supply a second value for ORDER BY). For my purposes that's fine. It could be resolved by adding one or two more procedural variables and using an IF function. Certainly beats the solutions I've seen most online.

Actually, since this is apparently a pretty common google search and if someone finds my solution there'll surely be someone who'd ask "how would you do it for same-level with IFs?" I made a quick test for proof of concept. Here we go.
SET @count=(SELECT COUNT(*) FROM test);
SET @row=-1;
SET @last=0;
SET @lastperc=0;
UPDATE test SET percentile=IF(number=@last, -- if the number is the same as last time
  IF((@row:=@row+1)!='x',@lastperc,0), -- increase row, set percentile to @lastperc
  IF((@last:=number)!='x',(@lastperc:=((@row:=@row+1)/@count)),0) -- else set @last to this number, set @lastperc to new percentile, increase row
) ORDER BY number;
This operates on a table named 'test' with columns 'number' and 'percentile', and gives results where the percentile is "percentage of values that are lower", eg.
+--------+------------+
| number | percentile |
+--------+------------+
|      1 |          0 |
|      4 |        0.2 |
|      4 |        0.2 |
|     13 |        0.6 |
|     17 |        0.8 |
+--------+------------+
Also of note, if you're doing this in PHP or Perl then the 'SET' queries must each be done as a separate query before the main one. Probably best to remove the comments too for putting it in through a program.

I assume there's a better way of doing "update this procedural variable and don't incorporate it into the output in any way just now" than my horrible use of setting the variable and then comparing it against 'x' in an IF that thus goes on to be ignored, but I don't know what that way would be. Regardless of that horribleness, it remains that this is a bucketload faster than what seems to be the common standard method, "UPDATE test SET percentile=(SELECT COUNT(*) FROM test AS b WHERE b.number<test.number)/$count" (I'm allowing $count from a PHP or something because without that it's even more horrendous.) [23:35]

RavenBlack
Oops, misposted the first time, commenting to fix the static page.

Andrew
Congratulations on the engagement.
Add Comment:
Name:Comment: (max. 2048 characters)
Email:
Show Email: (if no website)
Website:
No HTML tags allowed.
(Antispam) What is 29 + 1?
Archives: Last 4 Days | June2001 | July2001 | August2001 | September2001 | October2001 | November2001 | December2001 | January2002 | February2002 | March2002 | April2002 | May2002 | June2002 | July2002 | August2002 | September2002 | October2002 | November2002 | December2002 | January2003 | February2003 | March2003 | April2003 | May2003 | June2003 | July2003 | August2003 | September2003 | October2003 | November2003 | December2003 | January2004 | February2004 | March2004 | April2004 | May2004 | June2004 | July2004 | August2004 | September2004 | October2004 | November2004 | December2004 | January2005 | February2005 | March2005 | April2005 | May2005 | June2005 | July2005 | August2005 | September2005 | October2005 | November2005 | January2006 | February2006 | March2006 | April2006 | May2006 | June2006 | July2006 | August2006 | September2006 | October2006 | November2006 | December2006 | January2007 | February2007 | March2007 | April2007 | May2007 | June2007 | July2007 | August2007 | September2007 | October2007 | November2007 | December2007 | January2008 | February2008 | March2008 | April2008 | May2008 | June2008 | July2008 | August2008 | September2008 | October2008 | November2008 | December2008 | January2009 | March2009 | April2009 | May2009 | July2009 | August2009 | September2009 | February2010 | March2010 | June2010 | July2010 | August2010 | September2010 | October2010 | November2010 | December2010 | February2011 | March2011 | April2011 | May2011 | June2011 | July2011 | August2011 | September2011 | October2011 | December2011 | March2012 | April2012 | May2012 | September2012 | December2012 | March2013 | April2013 | May2013 | June2013 | October2021