这是一个解决方案。这是蛮力,并使用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
包括功率放大器需要添加另一个CTE,然后在dealid和PA ID加入交易和PAS得到所有可能的组合。当两行都有重复时,你没有具体说明你想要发生什么,所以我只是猜测你会想要所有的组合。
我的DB2的版本没有'UNPIVOT',所以我会写一个递归查询(不能现在)拆分此。但是那种布局......请转动,折叠和破坏原始设计师。 –