这是一种解决方案。这是蛮力并使用 union all 来获取多个副本:
with incols as (
select (case when charindex(Dealid, ',') > 0
then left(DealId, charindex(Dealid, ',') - 1)
else DealId
end) as DealId1,
(case when charindex(Dealid, ',') > 0
then substring(DealId, charindex(DealId, ',') + 1, 100)
end) as DealId2,
(case when charindex(PAId, ',') > 0
then left(PAId, charindex(PAId, ',') - 1)
else PAId
end) as PAId1,
(case when charindex(PAId, ',') > 0
then substring(PAId, charindex(PAId, ',') + 1, 100)
end) as PAId2,
t.*
from t
),
deals as (
select (case when whichdeal = 1 then deal1 else deal2 end) as newdeal, t.*
from ((select *, 1 as whichdeal
from t
) union all
(select *, 2 as whichdeal
from t
where deal2 is not null
)) t
)
select newdeal as dealid, t.*
from deals
包括 PA 需要添加另一个 CTE,然后在 dealid 和 PA id 上加入交易和 PA 以获得所有可能的组合。当两行中都有重复项时,您没有准确指定要发生的事情,所以我只是猜测您会想要所有组合。