首页 > 解决方案 > DB2,SQL 下一个代码 = 修改描述 将实际 id 传递给子选择

问题描述

你好我还在学习sql,我有新的挑战。

如果代码 AMD 的描述由关键字开始,例如 /delete,我必须在最后一行删除后删除短语并插入描述。

例如对于代码 AMD 中的 transaction1 有关键字 delete 和 ab 所以我想删除 ab 所以如果步骤 AMD001 是最后一个结果将是 cdefgh。

完整示例(ISS 始终是第一步,在步骤 AMD 中发生变化)

交易编号 代码 描述
1 ISS001 abcdefgh
1 AMD001 删除/ab
1 AMD002 删除/c
1 AMD003 删除/天
1 AMD004 添加/电子

输出交易=1 描述= efghe。

我尝试使用子句案例。

with data as
(
select transaction_id,description  from table where code=ISS001
),
data1 as (
select transaction_id,
Case 
when code=AMD001 and description like 'ADD/%' then data.description || substr(table.descrption, 4, length(table.descrption-4))
when code=AMDD001 and description like 'DELETE/%' ........
end as first
from ......
),
data2 as 
(
select
transaction_id, 
Case 
when code=AMD002 and description like 'delete/%' then (select first from data1 where data1.transaction_id =data_2.transaction_id ) || substr(table.descrption, 4, length(table.descrption-4))
when code=AMDD002 and description like 'DELETE/%' ........
end as second from.....
)
select * from data2

我在将实际 transaction_id 传输到数据 2 上的子选择时遇到问题

(

select first from data1 where data1.transaction_id =data_2.transaction_id 

)。

它可能吗?你能帮助我吗 ?任何提示?

标签: sqldb2subquery

解决方案


推荐阅读