实际记录:
预期记录:
create table和insert语句here:-
create table t11
(
id number,
name varchar2(20),
standard varchar2(20),
sdt date
);
insert into t11 values(1,'Ramakrushna',to_char(1,'RN'), sysdate);
insert into t11 values(1,'Ramakrushna',to_char(2,'RN'), sysdate);
insert into t11 values(2,'Ramakrushna',to_char(1,'RN'),sysdate);
insert into t11 values(2,'Ramakrushna',to_char(5,'RN'),sysdate);
insert into t11 values(3,'Ramakrushna',to_char(1,'RN'),sysdate);
动态记录基于id,它应该与id一起生成8条记录。表中保存的记录如下,对于id 1记录可以是i和iii标准(罗马值),但id 1的预期记录将是,i、ii、iii、iv、v、vi、vii、viii,其他id也一样,基于表中保存的记录。
这是我的方法,使用with as子句。它几乎不需要修改。
with t01 as (
select rownum rn,id,name,standard,sdt from t11
),
t02 as (
select rownum rn ,id,name,to_char(to_char(l,'RN')) as standard,sdt,l
from t01
left outer join (select level l from dual connect by level <= ((select max(rn) from t01) + 6)) lev on 1 = 1 -- passed 2 as argument
),
t03 as (
select t02.id,t02.name,t02.standard,t02.sdt,
row_number() over(partition by standard order by 1) rnk
from t02 where not exists (select 'Y' from t01 where t01.standard = t02.standard)
)
select id,name,standard,sdt from t03 where rnk = 1
union
select id,name,standard,sdt from t1;
1条答案
按热度按时间v7pvogib1#
如果
name
以及sdt
对于每个id
,如示例数据中所示,那么一种简单的方法就是只选择不同的元组,并使用递归查询生成所有数字:我喜欢使用常用的表表达式而不是
connect by
,因为它们是标准的sql,并且在oraclce中支持了相当长的一段时间,但是逻辑与connect by
:db小提琴演示: