由于类型原因,Oracle SQL数据在月份内从行迁移到列失败
表2由于类型原因,Oracle SQL数据在月份内从行迁移到列失败,sql,oracle,gaps-and-islands,Sql,Oracle,Gaps And Islands,表2 TABLE1 CODE RATE1 type MONTH A 0 Acc1 201906 A 0 Acc1 201907 A 0 Acc1 201908 A 1 Acc1 201909 A 1 Acc1 201910 A 1 Acc1 201911 A 1 Acc1
TABLE1
CODE RATE1 type MONTH
A 0 Acc1 201906
A 0 Acc1 201907
A 0 Acc1 201908
A 1 Acc1 201909
A 1 Acc1 201910
A 1 Acc1 201911
A 1 Acc1 201912
A 1 Acc1 202001
A 1 Acc1 202002
A 1 Acc1 202003
A 1 Acc1 202004
A 1 Acc1 202005
A 1 Acc1 202006
A 1 Acc1 202007
A 1 Acc1 202008
A 1 Acc1 202009
A 0 Acc2 201906
A 0 Acc2 201907
A 0 Acc2 201908
A 1 Acc2 201909
A 1 Acc2 201910
A 1 Acc2 201911
A 1 Acc2 201912
A 1 Acc2 202001
A 1 Acc2 202002
A 1 Acc2 202003
A 1 Acc2 202004
A 1 Acc2 202005
A 1 Acc2 202006
A 1 Acc2 202007
A 1 Acc2 202008
A 1 Acc2 202009
我正在将数据从旧系统迁移到新系统。作为每月维护的旧系统数据的一部分,如果数据更新且表包含一个月的一行,则将更新同一行。我将迁移到新闻系统,其中包含创建活动记录的开始日期和结束日期。所以在更新新数据时需要插入和更新旧的行结束日期
我有两个表需要加入,并根据rate1和rate2查找开始日期和结束日期。
我的第一个表包含所有月份的数据,第二个表数据可用,直到其在Dectivate上的活动数据不可用。如果可用率,我们认为为0。
我的预期产出
CODE RATE2 MONTH
A 10 202001
A 10 202002
A 10 202003
A 10 202004
但我的成绩低于23排
CODE RATE1 RATE2 Type START_DT END_DT
A 0 0 Acc1 201906 201908
A 1 0 Acc1 201909 201912
A 1 10 Acc1 202001 202004
A 1 0 Acc1 202005 202009
A 0 0 Acc2 201906 201908
A 1 0 Acc2 201909 201912
A 1 10 Acc2 202001 202004
A 1 0 Acc2 202005 202009
请在此URL中找到我的查询
提前谢谢
如果您需要更多信息,请发表意见您已接近解决此差距和孤岛分配。问题在于列
类型
。它需要转到两个窗口函数的分区依据
子句,因此它与外部分组依据
一致
这将为您提供所需的结果:
select code1, rate1, rate2, type, min(month) start_dt,
case when row_number() over(partition by code1 order by max(month) desc) = 1 then 999912 else max(month) end end_dt
from (
select t1.month, code1, rate1, rate2, type,
row_number() over(partition by code1 order by t1.month) rn1,
row_number() over(partition by code1, type, rate1, rate2, type order by t1.month) rn2
from table1 t1 left join table2 t2 on t1.code1 = t2.code2 and t1.month = t2.month
) t
group by code1, rate1, rate2, type, rn1 - rn2
order by start_dt
注:
- 我不确定您希望在外部查询中使用日期转换实现的逻辑,而且它似乎与期望的结果不一致,因此我将其删除
- 使用子查询中的所有列所属的表来限定它们;这避免了歧义
- 我还修复了外部
子句,以便按所需顺序返回结果,并使用order by
将缺少的coalesce()
s默认为rate2
0
select code1, rate1, coalesce(rate2, 0), type, min(month) start_dt, max(month) end_dt
from (
select t1.month, t1.code1, t1.rate1, t2.rate2, type,
row_number() over(partition by t1.code1, t1.type order by t1.month) rn1,
row_number() over(partition by t1.code1, t1.type, t1.rate1, t2.rate2 order by t1.month) rn2
from table1 t1
left join table2 t2 on t1.code1 = t2.code2 and t1.month = t2.month
) t
group by code1, rate1, rate2, type, rn1 - rn2
order by type, start_dt
代码1 |速率1 |聚结(速率2,0)|类型|开始|结束|
:---- | ----: | ----------------: | :--- | -------: | -----:
A | 0 | 0 | Acc1 | 201906 | 201908
A | 1 | 0 | Acc1 | 201909 | 201912
A | 1 | 10 | Acc1 | 202001 | 202004
A | 1 | 0 | Acc1 | 202005 | 202009
A | 0 | 0 | Acc2 | 201906 | 201908
A | 1 | 0 | Acc2 | 201909 | 201912
A | 1 | 10 | Acc2 | 202001 | 202004
A | 1 | 0 | Acc2 | 202005 | 202009
您已经非常接近于解决此差距和孤岛分配。问题在于列
类型
。它需要转到两个窗口函数的分区依据
子句,因此它与外部分组依据
一致
这将为您提供所需的结果:
select code1, rate1, rate2, type, min(month) start_dt,
case when row_number() over(partition by code1 order by max(month) desc) = 1 then 999912 else max(month) end end_dt
from (
select t1.month, code1, rate1, rate2, type,
row_number() over(partition by code1 order by t1.month) rn1,
row_number() over(partition by code1, type, rate1, rate2, type order by t1.month) rn2
from table1 t1 left join table2 t2 on t1.code1 = t2.code2 and t1.month = t2.month
) t
group by code1, rate1, rate2, type, rn1 - rn2
order by start_dt
注:
- 我不确定您希望在外部查询中使用日期转换实现的逻辑,而且它似乎与期望的结果不一致,因此我将其删除
- 使用子查询中的所有列所属的表来限定它们;这避免了歧义
- 我还修复了外部
子句,以便按所需顺序返回结果,并使用order by
将缺少的coalesce()
s默认为rate2
0
select code1, rate1, coalesce(rate2, 0), type, min(month) start_dt, max(month) end_dt
from (
select t1.month, t1.code1, t1.rate1, t2.rate2, type,
row_number() over(partition by t1.code1, t1.type order by t1.month) rn1,
row_number() over(partition by t1.code1, t1.type, t1.rate1, t2.rate2 order by t1.month) rn2
from table1 t1
left join table2 t2 on t1.code1 = t2.code2 and t1.month = t2.month
) t
group by code1, rate1, rate2, type, rn1 - rn2
order by type, start_dt
代码1 |速率1 |聚结(速率2,0)|类型|开始|结束|
:---- | ----: | ----------------: | :--- | -------: | -----:
A | 0 | 0 | Acc1 | 201906 | 201908
A | 1 | 0 | Acc1 | 201909 | 201912
A | 1 | 10 | Acc1 | 202001 | 202004
A | 1 | 0 | Acc1 | 202005 | 202009
A | 0 | 0 | Acc2 | 201906 | 201908
A | 1 | 0 | Acc2 | 201909 | 201912
A | 1 | 10 | Acc2 | 202001 | 202004
A | 1 | 0 | Acc2 | 202005 | 202009
您可以连接这两个表,然后使用
MATCH\u RECOGNIZE
:
选择代码,
比率1,
比率2,
类型,
第一个月,
上个月
从(
选择t1.code,
t1.1,
合并(t2.rate2,0)为rate2,
t1.1型,
一个月
来自表1 t1
左外连接表2 t2
ON(t1.code=t2.code和t1.month=t2.month)
)
相配(
按类型划分
按月订购
措施
第一个(代码)作为代码,
第一(费率1)作为费率1,
第一(评级2)为评级2,
第一(月)作为第一个月,
上(月)与上(月)相同
每场比赛一排
模式(第一行相同的费率*)
定义
与相同的利率(相同的利率.rate1=上一个(相同的利率.rate1)
和相同的利率.rate2=上一个(相同的利率.rate2))
)
订单类型,第一个月;
其中,对于您的示例数据:
创建表table1(代码、费率1、类型、月份),如下所示
从DUAL UNION ALL中选择'A',0',Acc1',201906
从DUAL UNION ALL中选择'A',0',Acc1',201907
从DUAL UNION ALL中选择'A',0',Acc1',201908
从DUAL UNION ALL中选择'A',1',Acc1',201909
从DUAL UNION ALL中选择'A',1',Acc1',201910
从DUAL UNION ALL(双联管接头全部)中选择'A',1',Acc1',201911
从DUAL UNION ALL中选择'A',1',Acc1',201912
从DUAL UNION ALL中选择'A',1',Acc1',202001
从DUAL UNION ALL(双联管接头全部)中选择'A',1',Acc1',202002
从DUAL UNION ALL中选择'A',1',Acc1',202003
从DUAL UNION ALL中选择'A',1',Acc1',202004
从DUAL UNION ALL中选择'A',1',Acc1',202005
从DUAL UNION ALL中选择'A',1',Acc1',202006
从DUAL UNION ALL中选择'A',1',Acc1',202007
从DUAL UNION ALL中选择'A',1',Acc1',202008
从DUAL UNION ALL中选择'A',1',Acc1',202009
从DUAL UNION ALL中选择'A',0',Acc2',201906
从DUAL UNION ALL中选择'A',0',Acc2',201907
从DUAL UNION ALL中选择'A',0',Acc2',201908
从DUAL UNION ALL中选择'A',1',Acc2',201909
从DUAL UNION ALL中选择'A',1',Acc2',201910
从DUAL UNION ALL(双联管接头全部)中选择'A',1',Acc2',201911
从DUAL UNION ALL中选择'A',1',Acc2',201912
s