简体   繁体   中英

how to use median as a analytic function (oracle SQL)

Can you explain why the following works:

select recdate,avg(logtime) 
over 
(ORDER BY recdate rows between 10 preceding and 0 following) as logtime 
from v_download_times;

and the following doesn't

select recdate,median(logtime)
over 
(ORDER BY recdate rows between 10 preceding and 0 following) as logtime
 from v_download_times;

(median instead of avg)

I get an ORA-30487 error.

and I would be grateful for a workaround.

The error message is ORA-30487: ORDER BY not allowed here . And sure enough, if we consult the documentation for the MEDIAN function it says:

"You can use MEDIAN as an analytic function. You can specify only the query_partition_clause in its OVER clause."

But it is not redundant if you only want to take it from a certain number of rows preceding the current one. A way around may be limiting your data set just for the median purpose, like

select 
median(field) over (partition by field2) 
from ( select * from dataset 
        where period_back between 0 and 2 )

MEDIAN doesn't allow an ORDER BY clause. As APC points out in his answer, the documentation tells us we can only specify the query_partition_clause.

ORDER BY is redundant as we're looking for the central value -- it's the same regardless of order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM