如何根据行优先级将此 Oracle 结果集压缩为值,忽略空值?
我将尽可能简化问题:
我有一个 oracle 表:
row_priority, col1, col2, col3
0, .1, 100, {null}
12, {null}, {null}, 3
24, .2, {null}, {null}
期望的结果:
col1, col2, col3
.2, 100, 3
因此,根据行的优先级,它会覆盖先前的行值(如果给定)。
我正在尝试使用表上的分析函数来找出解决方案,但它只是没有表现......
我尝试:
select last_value(col1 ignore nulls) over () col1,
last_value(col2 ignore nulls) over () col2,
last_value(col3 ignore nulls) over () col3
from (select * from THE_TABLE order by row_priority)
where rownum = 1
或相反:
select first_value(col1 ignore nulls) over () col1,
first_value(col2 ignore nulls) over () col2,
first_value(col3 ignore nulls) over () col3
from (select * from THE_TABLE order by row_priority desc)
where rownum = 1
并且似乎都没有忽略空值。 有什么提示吗?
I'll simplify the problem as much as possible:
I have an oracle table:
row_priority, col1, col2, col3
0, .1, 100, {null}
12, {null}, {null}, 3
24, .2, {null}, {null}
Desired result:
col1, col2, col3
.2, 100, 3
So according to the priority of the row, it overrides previous row values, if given.
I'm attempting to work out a solution using analytical functions over the table, but it just isn't behaving...
I try:
select last_value(col1 ignore nulls) over () col1,
last_value(col2 ignore nulls) over () col2,
last_value(col3 ignore nulls) over () col3
from (select * from THE_TABLE order by row_priority)
where rownum = 1
or the inverse:
select first_value(col1 ignore nulls) over () col1,
first_value(col2 ignore nulls) over () col2,
first_value(col3 ignore nulls) over () col3
from (select * from THE_TABLE order by row_priority desc)
where rownum = 1
And neither seem to ignore nulls. Any hints?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要将 rownum = 1 放在分析查询之外
,这会导致(使用上面的值):
You need to put rownum = 1 OUTSIDE the analytical query
which results in (using your values above):
COALESCE 函数在这里可能对您有帮助。 也许就像...
The COALESCE function may be of help to you here. Perhaps like ...
替代方案:
其性能可能与解析版本不同; 是好是坏取决于您的数据和环境。
An alternative:
The performance of this may be different from the analytic version; whether it is better or worse depends on your data and environment.