-- código #3
with
PargPar as (
SELECT ID_sequencia, T.sequencia, S1.ItemNumber as seqPar, S1.Item as Par
from tabela as T
cross apply dbo.DelimitedSplit8K (T.sequencia, ';') as S1
),
ElementogElemento as (
SELECT P.ID_Sequencia, P.sequencia, P.seqPar,
S2.ItemNumber as seqElemento, S2.Item as Elemento
from PargPar as P
cross apply dbo.DelimitedSplit8K (P.Par, ',') as S2
)
SELECT ID_Sequencia, sequencia, seqPar, seqElemento, Elemento
from ElementogElemento
order by ID_Sequencia, seqPar;
-- código #4
with
PargPar as (
SELECT ID_sequencia, T.sequencia, S1.ItemNumber as seqPar, S1.Item as Par
from tabela as T
cross apply dbo.DelimitedSplit8K (T.sequencia, ';') as S1
),
ElementogElemento as (
SELECT P.ID_Sequencia, P.sequencia, P.seqPar,
S2.ItemNumber as seqElemento, S2.Item as Elemento
from PargPar as P
cross apply dbo.DelimitedSplit8K (P.Par, ',') as S2
)
SELECT ID_Sequencia,
string_agg (Elemento, ',') within group (order by seqPar)
from ElementogElemento
where seqElemento = 2
group by ID_Sequencia;
-- código #5
with
gPar as (
SELECT ID_sequencia, T.sequencia, S1.ItemNumber as seqPar, S1.Item as Par
from tabela as T
cross apply dbo.DelimitedSplit8K (T.sequencia, ';') as S1
),
gElemento as (
SELECT P.ID_Sequencia, P.sequencia, P.seqPar,
S2.ItemNumber as seqElemento, S2.Item as Elemento
from gPar as P
cross apply dbo.DelimitedSplit8K (P.Par, ',') as S2
)
SELECT E.ID_Sequencia,
--string_agg (Elemento, ',') within group (order by seqPar)
stuff ((SELECT ',' + T.Elemento
from gElemento as T
where T.ID_Sequencia = T.ID_Sequencia
and T.seqElemento = 2
order by T.seqPar
for xml path(''), TYPE).value('.', 'varchar(max)'),
1, 1, '') as novaSequencia
from gElemento as E
where E.seqElemento = 2
group by E.ID_Sequencia;