with sample_data as (select '26.03.2015 14:10' as adate, 4 as type, 40 as object, 111 as barcode from dual union all
select '26.03.2015 14:09' as adate, 1 as type, 55 as object, 222 as barcode from dual union all
select '26.03.2015 14:08' as adate, 2 as type, 33 as object, 777 as barcode from dual union all
select '26.03.2015 14:08' as adate, 2 as type, 34 as object, null as barcode from dual union all
select '26.03.2015 13:20' as adate, 3 as type, 41 as object, null as barcode from dual union all
select '26.03.2015 12:00' as adate, 1 as type, 56 as object, 444 as barcode from dual union all
select '26.03.2015 11:59' as adate, 2 as type, 37 as object, 555 as barcode from dual union all
select '26.03.2015 11:59' as adate, 2 as type, 48 as object, null as barcode from dual)
select
adate, type, object, barcode
from sample_data
where type in (1, 2);
类型1已完成构建,类型2是程序集的一部分
我需要下一个结果
55 | 222 | 26.03.2015 14:08 | 33
55 | 222 | 26.03.2015 14:08 | 34
56 | 444 | 26.03.2015 11:59 | 37
56 | 444 | 26.03.2015 11:59 | 38
所以我们看到哪个对象 55 包含子对象 33 , 34 和两个日期= 26.03.2015 14:08 和对象 56 包含子对象 37 , 38 和两个日期= 26.03.2015 11:59
一开始我知道两个参数 - 完成建筑的日期和条形码,例如我的数据 - 日期 26.03.2015 14:09 和条形码 222 或日期< strong> 26.03.2015 12:00 和条形码 444
答案 0 :(得分:0)
此查询完成此任务。子查询T1将“类型1”与“类型2”连接起来。 T2使用函数row_number()枚举它们。 上次查询仅过滤rn = 1的行。
with t1 as (
select t.*, prior object pob, prior barcode pbc,
prior adate pad, level, sys_connect_by_path(object, '-') pth
from sample_data t where level=2
connect by prior adate > adate and prior type = 1 and type = 2),
t2 as (select t1.*,
row_number() over (partition by object order by pad) rn from t1 )
select pob, pbc, adate, object from t2 where rn = 1
结果:
POB PBC ADATE OBJECT
---------- ---------- ------------------- ----------
55 222 2015-03-26 14:08:00 33
55 222 2015-03-26 14:08:00 34
56 444 2015-03-26 11:59:00 37
56 444 2015-03-26 11:59:00 48
答案 1 :(得分:0)
您必须定义组,然后才能将项目分配给组
with sample_data as (select '26.03.2015 14:10' as adate, 4 as type, 40 as object, 111 as barcode from dual union all
select '26.03.2015 14:09' as adate, 1 as type, 55 as object, 222 as barcode from dual union all
select '26.03.2015 14:08' as adate, 2 as type, 33 as object, 777 as barcode from dual union all
select '26.03.2015 14:08' as adate, 2 as type, 34 as object, null as barcode from dual union all
select '26.03.2015 13:20' as adate, 3 as type, 41 as object, null as barcode from dual union all
select '26.03.2015 12:00' as adate, 1 as type, 56 as object, 444 as barcode from dual union all
select '26.03.2015 11:59' as adate, 2 as type, 37 as object, 555 as barcode from dual union all
select '26.03.2015 11:59' as adate, 2 as type, 48 as object, null as barcode from dual),
sd_groups as (select adate, lag(adate, 1) over (order by adate) adate_last, object, barcode from (
select * from sample_data where type = 1
union all select min(adate), null, null, null from sample_data))
select sg.object, sg.barcode, sd.adate, sd.object
from sample_data sd, sd_groups sg
where sd.type = 2 and sd.adate between sg.adate_last and sg.adate;