Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Median function in 10gR2

756666Feb 26 2010 — edited Feb 26 2010
I am seeing some unexpected behavior using the median function applied to a set of distinct values returned by a subquery. I didnt find an explanation by searching the web; hence the question here. These queries are on a 10gR2 database.

In both cases below, the select distinct should return 1,2,3,4,5; for a median of 3.

However the median is different when the 1,2 are repeated

This query returns 2 as median -

SELECT median(prob)
FROM (SELECT DISTINCT (prob) AS prob
FROM (SELECT 1 AS prob
FROM dual
UNION ALL
SELECT 2 AS prob
FROM dual
UNION ALL
SELECT 3 AS prob
FROM dual
UNION ALL
SELECT 4 AS prob
FROM dual
UNION ALL
SELECT 5 AS prob
FROM dual
UNION ALL
SELECT 1 AS prob
FROM dual
UNION ALL
SELECT 2 AS prob FROM dual) sub);

This query returns 3 as the median.

SELECT median(prob)
FROM (SELECT DISTINCT (prob) AS prob
FROM (SELECT 1 AS prob
FROM dual
UNION ALL
SELECT 2 AS prob
FROM dual
UNION ALL
SELECT 3 AS prob
FROM dual
UNION ALL
SELECT 4 AS prob
FROM dual
UNION ALL
SELECT 5 AS prob FROM dual) sub);

What am I missing about the median or the distinct function to cause this different behavior? My expectation was to get the same median - 3 - in both cases since the subquery that the median uses returns the same values in both.

Thanks in advance for any insight on this.

Comments

Tubby
Drop index un1 ;
create index un1 on xx_test (a1,a2,a3) ;
586651
I dont want to drop the index is it possible to alter?
659537
yes you can alter index with "alter index" syntax to change.
You can alter index for :-
Rebuild or coalesce an existing index
Deallocate unused space or allocate a new extent
Specify parallel execution (or not) and alter the degree of parallelism
Alter storage parameters or physical attributes
Specify LOGGING or NOLOGGING
Enable or disable key compression
Mark the index unusable
Start or stop the monitoring of index usage

-----You cannot alter an index's column structure.---------

For more details check in the documentation
http://download.oracle.com/docs/cd/B10501_01/server.920/a96521/indexes.htm#518
659537
yes you can alter index with "alter index" syntax to change.
You can alter index for :-
Rebuild or coalesce an existing index
Deallocate unused space or allocate a new extent
Specify parallel execution (or not) and alter the degree of parallelism
Alter storage parameters or physical attributes
Specify LOGGING or NOLOGGING
Enable or disable key compression
Mark the index unusable
Start or stop the monitoring of index usage

-----You cannot alter an index's column structure.---------

For more details check in the documentation
http://download.oracle.com/docs/cd/B10501_01/server.920/a96521/indexes.htm#518
Karthick2003
[ALTER INDEX|http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_1008.htm#SQLRF00805] is beautifully documented.

Thanks,
Karthick.
1 - 5
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Mar 26 2010
Added on Feb 26 2010
9 comments
1,901 views