Orcl 数据库SQL求教,为null的值补足上一位,请各位大神指点

般若 2018-11-08 11:57:53
...全文
57 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
般若 2018-11-08
  • 打赏
  • 举报
回复
是因为SQL> col id format 99999999;
SQL> col num format 99999999;
SQL> col new_num format 99999999; 这个的原因吗?
我在pl/sql里面创建表 test 后,直接执行的 select id,
name,
num,
nvl(num,lag(num, 1) ignore nulls over(order by id)) new_num
from test t;
般若 2018-11-08
  • 打赏
  • 举报
回复

感谢楼上的答复,但是和自己想要的结果有点偏差。如上图
卖水果的net 2018-11-08
  • 打赏
  • 举报
回复


SQL> create table test(id int, name varchar(10), num int);
Table created
SQL> begin
2 insert into test values(1, 'xx', 1);
3 insert into test values(2, 'xx', 11);
4 insert into test values(3, 'xx', null);
5 insert into test values(4, 'xx', null);
6 insert into test values(5, 'xx', 45);
7 insert into test values(6, 'xx', 56);
8 insert into test values(7, 'xx', null);
9 insert into test values(8, 'xx', 59);
10 end;
11 /
PL/SQL procedure successfully completed
SQL> col id format 99999999;
SQL> col num format 99999999;
SQL> col new_num format 99999999;
SQL> select id,
2 name,
3 num,
4 nvl(num,lag(num, 1) ignore nulls over(order by id)) new_num
5 from test t;
ID NAME NUM NEW_NUM
-------- ---------- -------- --------
1 xx 1 1
2 xx 11 11
3 xx 11
4 xx 11
5 xx 45 45
6 xx 56 56
7 xx 56
8 xx 59 59
8 rows selected
SQL> drop table test purge;
Table dropped

SQL>
般若 2018-11-08
  • 打赏
  • 举报
回复
是可以的,发现对于 数据稍微复杂的操作,用到的排序函数比较多呀,以前就只用过rownum over 这样的。 谢谢了
卖水果的net 2018-11-08
  • 打赏
  • 举报
回复
你再看一下 2# 的回复,我之前编辑过一次,估计你看的是之前的。

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧