title: (SQL) How to apply case statement with percentile to calculate median on a group by
body: <p>I am using case statement to calculate statistics on certain criteria like following</p>
  
  <pre><code>avg(case when det.detection_time &gt;0 then det.blocked_time end) 
  
  and
  
  sum(case when det.detection_time  =0 then 1 else 0 end) 
  </code></pre>
  
  <p><strong>How should I apply case statement to get median.</strong>.</p>
  
  <p>Currently I am getting median like following:</p>
  
  <pre><code>PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER by det.blocked_time)
  </code></pre>
  
  <p>How should I add condition "case when det.detection_time >0"  to the PERCENTILE_CONT block to get median for group where det.detection_time >0 </p>