這是一個解決方案。這是蠻力,並使用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',所以我會寫一個遞歸查詢(不能現在)拆分此。但是那種佈局......請轉動,摺疊和破壞原始設計師。 –