这是一个解决方案。它是野蛮的武力, 并使用工会所有带来多份副本:
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, 然后在交易和 PA id 上加入交易和 PA 来获得所有可能的组合。 当两行都出现重复时, 您没有具体说明您想要发生什么, 所以我只是猜测您想要所有的组合 。