我的分析函数FirstValue有问题:(语法:
FIRST_VALUE(TAble1.Column2 IGNORE NULLS) OVER (PARTITION BY Column1 ORDER BY Column3 DESC)
示例:
Column1 Column2 Column3
1 A 01/01/2012
1 (NULL) 02/01/2012
1 (NULL) 03/01/2012
我想使用上面描述的分析函数检索一行。
Column1 Column2 Column3
1 A 01/01/2012
问题是Oracle检索了两行,一行为Null,另一行在列2 中为值"A"
你能帮我解决这个问题吗?
向致以最诚挚的问候
这个问题有点老,但如果其他人在谷歌上搜索并完全陷入困境,请发布答案。
甲骨文默认的窗口化行为是罪魁祸首。
放置
range between unbounded preceding and unbounded following
在您的order by
条款之后
也就是说,
FIRST_VALUE(TAble1.Column2 IGNORE NULLS) OVER (PARTITION BY Column1 ORDER BY Column3 DESC range between unbounded preceding and unbounded following)
发生这种情况是因为组中第一个非null值在第1列和第2列的位置不同。
first_value函数与distinct的组合不是此类查询的解决方案。
您可以使用row_number函数:
select * from (
select
row_number() OVER (PARTITION BY Column1 ORDER BY Column3 DESC) as rnk,
FIRST_VALUE(TAble1.Column1 IGNORE NULLS)
OVER (PARTITION BY Column1 ORDER BY Column3 DESC) as column1,
FIRST_VALUE(TAble1.Column2 IGNORE NULLS)
OVER (PARTITION BY Column1 ORDER BY Column3 DESC) as column2,
column3
from your table
)
where rnk = 1