无法使用 Oracle 的 window 函数中的 ORDER BY 子句
Unable to user an ORDER BY clause in a window function with Oracle
我有一个查询,在 window 函数中没有 ORDER BY
子句也能正常工作:
select
"TABLE_NAME",
"DENSITY",
"NUM_DISTINCT",
ROWNUM,
median(DENSITY) OVER (PARTITION BY table_name )
from ALL_TAB_COLUMNS a
where 1=1
and owner = 'SYS'
and table_name='CARRY'
and "NUM_DISTINCT" < 1000
and DENSITY < 1
AND num_nulls = 0
但我确实需要这个 order by
子句来获取我需要的格式的数据。如果我添加 order by
,我会收到这个奇怪的错误消息:
ORA-30487: ORDER BY not allowed here
30487. 00000 - "ORDER BY not allowed here"
*Cause: DISTINCT functions and RATIO_TO_REPORT cannot have an ORDER BY
*Action:
Error at Line: 6 Column: 47
这是完整的 SQL 以及订单:
select
"TABLE_NAME",
"DENSITY",
"NUM_DISTINCT",
ROWNUM,
median(DENSITY) OVER (PARTITION BY table_name ORDER BY "DENSITY")
from ALL_TAB_COLUMNS a
where 1=1
and owner = 'DEANZA'
and table_name='CARRIER_A'
and "NUM_DISTINCT" < 1000
and DENSITY < 1
AND num_nulls = 0
如 documentation 中所述,对于 MEDIAN
,您不能在其 OVER
子句中使用 ORDER BY
。
MEDIAN
将采用数字或日期时间值和 returns 中间值或在值 排序后成为中间值的插值 .所以无论如何都不需要使用ORDER BY
。
我有一个查询,在 window 函数中没有 ORDER BY
子句也能正常工作:
select
"TABLE_NAME",
"DENSITY",
"NUM_DISTINCT",
ROWNUM,
median(DENSITY) OVER (PARTITION BY table_name )
from ALL_TAB_COLUMNS a
where 1=1
and owner = 'SYS'
and table_name='CARRY'
and "NUM_DISTINCT" < 1000
and DENSITY < 1
AND num_nulls = 0
但我确实需要这个 order by
子句来获取我需要的格式的数据。如果我添加 order by
,我会收到这个奇怪的错误消息:
ORA-30487: ORDER BY not allowed here
30487. 00000 - "ORDER BY not allowed here"
*Cause: DISTINCT functions and RATIO_TO_REPORT cannot have an ORDER BY
*Action:
Error at Line: 6 Column: 47
这是完整的 SQL 以及订单:
select
"TABLE_NAME",
"DENSITY",
"NUM_DISTINCT",
ROWNUM,
median(DENSITY) OVER (PARTITION BY table_name ORDER BY "DENSITY")
from ALL_TAB_COLUMNS a
where 1=1
and owner = 'DEANZA'
and table_name='CARRIER_A'
and "NUM_DISTINCT" < 1000
and DENSITY < 1
AND num_nulls = 0
如 documentation 中所述,对于 MEDIAN
,您不能在其 OVER
子句中使用 ORDER BY
。
MEDIAN
将采用数字或日期时间值和 returns 中间值或在值 排序后成为中间值的插值 .所以无论如何都不需要使用ORDER BY
。