title: Calculating 50th percentile in PostgreSQL
body: <p>I have a table containing 100 thousand rows of the following format: </p>
  
  <h2>UserID  -  Rank  -  Value   - Primary Key</h2>
  
  <h2>1236  -----    00  ----  233.4  --------   1</h2>
  
  <h2>1377  -----    00  ----    150.6 --------    2</h2>
  
  <h2>1287  -----    01  ----   167 ----------      3</h2>
  
  <h2>1343  -----  01   ----   134.5 --------    4</h2>
  
  <h2>1284  -----   01   ----   155.2 --------    5</h2>
  
  <h2>1386  -----    02   ----   302  -----------     6</h2>
  
  <h2>1372  -----    02  ----    108.3     7</h2>
  
  <p>I need to compute the 50th percentile, grouped by the Rank, meaning that I need to obtain such results:</p>
  
  <h2>Rank  -----   50th percentile</h2>
  
  <h2>00   ----------    …</h2>
  
  <h2>01   ----------    …</h2>
  
  <h2>02   ----------   …</h2>
  
  <p>The 50th percentile of every rank is that row with primary key equals to 0.5 times the count of rows of every rank (i.e, 0.5 times count where rank = 01, 0.5 times count where rank = 02, …etc). However, bear in mind that after I get the 50th percentile of Rank 00, the 50th percentile of Rank 01 will be located at 0.5*(count of rows with rank 01) + (count of rows with rank 00) and so on..</p>
  
  <p>How do I do this in <strong>PostgreSQL</strong>? Your help is greatly appreciated.</p>