0

我有一张emp如下表

empnum deptno salary
----- ------ ------
1      null    null
null   2       null
null   null    null
null   null    3
5      null    4
null   6       1  

我希望结果集应该像

result
------
1
2
0
3
5
4
6
1

如果有3 nulls它应该显示0
如果有only one null它应该以插入它们的相同顺序显示其他两个值,
如果有“2个空值”那么它应该在结果集中显示一个值。

我试过了

select rs from  
((select decode(empno||deptno||sal,null,0,substr(empno||deptno||sal,1,1)as rs,rowid as ri from emp)
union  
((select decode(empno||deptno||sal,null,0,substr(empno||deptno||sal,2,1)as rs,rowid as ri from emp)  
minus  
(select decode(empno||deptno||sal,null,0,substr(empno||deptno||sal,1,1)as rs,rowid as ri from emp)))  
where rs is not null  
order by ri,rs;

上面的查询给出的结果为

rs
--
1
2
0
3
4
5
1
6

任何人都可以帮助我获得如上所述的确切结果集。

提前致谢..

4

1 回答 1

1

这个答案很大程度上取决于您的表是如何创建的。它需要使用 ROWDEPENDENCIES 创建,并且每次插入后需要有一个 COMMIT 以确保保持顺序。

这对于现实世界的数据可能没有用。如果您没有在表中存储记录插入时间的时间戳,则无法确定列的顺序。

带有样本表和数据:

create table emp
(empnum int, deptno int, salary int) ROWDEPENDENCIES;
insert into emp (empnum, deptno, salary) values
(1,      null,    null); 
commit;
insert into emp (empnum, deptno, salary) values
(null,   2,       null);
commit;
insert into emp (empnum, deptno, salary) values
(null,   null  ,  null); 
commit;
insert into emp (empnum, deptno, salary) values
(null ,  null ,   3); 
commit;
insert into emp (empnum, deptno, salary) values
(5     , null,    4);
commit;
insert into emp (empnum, deptno, salary) values
(null   ,6 ,      1  ); 
commit;

create table emp2
(empnum int, deptno int, salary int, rowscn int) ROWDEPENDENCIES;
insert into emp2
select empnum, deptno, salary, ora_rowscn from emp;

然后,您可以使用ORA_ROWSCN伪列来确定插入日期:

with countqry as
(select empnum, deptno, salary, 
        rowscn,
        decode(empnum, null, 0, 1) e,
        decode(deptno, null, 0, 1) d,
        decode(salary, null, 0, 1) s    
from emp2)
select 0 as val, rowscn 
from countqry where e+d+s = 0
union  all
select coalesce(empnum, deptno, salary) as val, 
       rowscn
from countqry where e+d+s = 1
union all 
select val, rowscn from
(
select * from 
  (select empnum, deptno, salary, rowscn 
    from countqry where e+d+s >= 2)
unpivot (val for x in (empnum as '1', deptno as '2', salary as '3'))
order by rowscn, val
)
order by rowscn

这是一个关于 SQLFiddle 的工作示例。

于 2012-06-26T18:29:56.300 回答