Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Mailing Lists -> Oracle-L -> RE: sql/function to calculate the median

RE: sql/function to calculate the median

From: Pardee, Roy E <roy.e.pardee_at_lmco.com>
Date: Tue, 22 Apr 2003 11:31:53 -0800
Message-ID: <F001.005872E7.20030422113153@fatcity.com>


(Since I've not seen any responses to this I'll give it a go.)

If you can use OLAP functions, have a look at PERCENTILE_CONT--that gives you any percentile you need--median is of course 50th.

There is a nice discussion of this & several candidate solutions in Celko's _SQL For Smarties_.

I was thinking you might be able to do something quick-n-dirty by counting the number of non-null values in the column whose median you need & then selecting out the value that falls at rownum = round(count/2,0) in a properly ordered result set. But I couldn't get that to work on a test table, so I won't post my code.

HTH,
-Roy

Roy Pardee
Programmer/Analyst
SWFPAC Lockheed Martin IT
Extension 8487

-----Original Message-----

Sent: Monday, April 21, 2003 12:12 PM
To: Multiple recipients of list ORACLE-L

Hi All,

Oracle 8.0.5
Does anyone have sql statement/functin to calculate the median?

Thanks
Rick

--

Please see the official ORACLE-L FAQ: http://www.orafaq.net
--

Author:
  INET: Rick_Cale_at_teamhealth.com

Fat City Network Services    -- 858-538-5051 http://www.fatcity.com
San Diego, California        -- Mailing list and web hosting services

---------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message
to: ListGuru_at_fatcity.com (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing).
--

Please see the official ORACLE-L FAQ: http://www.orafaq.net
--

Author: Pardee, Roy E
  INET: roy.e.pardee_at_lmco.com
Fat City Network Services    -- 858-538-5051 http://www.fatcity.com
San Diego, California        -- Mailing list and web hosting services

---------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message
to: ListGuru_at_fatcity.com (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing). Received on Tue Apr 22 2003 - 14:31:53 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US