Skip to main content
correção código #5
Fonte Link
José Dız
  • 7mil
  • 2
  • 18
  • 30
-- código #5 v2
-- Autor: José Diz 
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 = TE.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;
-- código #5
-- Autor: José Diz 
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;
-- código #5 v2
-- Autor: José Diz 
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 = E.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;
adicionou 37 caracteres ao conteúdo
Fonte Link
José Dız
  • 7mil
  • 2
  • 18
  • 30
-- código #4
-- Autor: José Diz 
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 ID_Sequencia, 
       string_agg (Elemento, ',') within group (order by seqPar)
  from gElemento
  where seqElemento = 2
  group by ID_Sequencia;

Como a função STRING_AGG somente funciona a partir da versão 2017 do SQL Server, ela pode ser substituída pela técnica blackbox xml nas versões anteriores do SQL Server. Detalhes no artigo Concatenação de várias linhas de uma mesma coluna.

-- código #5
-- Autor: José Diz 
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;
-- código #4
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 ID_Sequencia, 
       string_agg (Elemento, ',') within group (order by seqPar)
  from gElemento
  where seqElemento = 2
  group by ID_Sequencia;

Como a função STRING_AGG somente funciona a partir da versão 2017 do SQL Server, ela pode ser substituída pela técnica blackbox xml. Detalhes no artigo Concatenação de várias linhas de uma mesma coluna.

-- 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;
-- código #4
-- Autor: José Diz 
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 ID_Sequencia, 
       string_agg (Elemento, ',') within group (order by seqPar)
  from gElemento
  where seqElemento = 2
  group by ID_Sequencia;

Como a função STRING_AGG somente funciona a partir da versão 2017 do SQL Server, ela pode ser substituída pela técnica blackbox xml nas versões anteriores do SQL Server. Detalhes no artigo Concatenação de várias linhas de uma mesma coluna.

-- código #5
-- Autor: José Diz 
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;
adicionou 1025 caracteres ao conteúdo
Fonte Link
José Dız
  • 7mil
  • 2
  • 18
  • 30
-- 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;
-- código #3
with
Par 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
),
Elemento as (
SELECT P.ID_Sequencia, P.sequencia, P.seqPar, 
       S2.ItemNumber as seqElemento, S2.Item as Elemento
  from Par as P
       cross apply dbo.DelimitedSplit8K (P.Par, ',') as S2
)
SELECT ID_Sequencia, sequencia, seqPar, seqElemento, Elemento
  from Elemento 
  order by ID_Sequencia, seqPar;
-- código #4
with
Par 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
),
Elemento as (
SELECT P.ID_Sequencia, P.sequencia, P.seqPar, 
       S2.ItemNumber as seqElemento, S2.Item as Elemento
  from Par as P
       cross apply dbo.DelimitedSplit8K (P.Par, ',') as S2
)
SELECT ID_Sequencia, 
       string_agg (Elemento, ',') within group (order by seqPar)
  from Elemento
  where seqElemento = 2
  group by ID_Sequencia;
-- código #3
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 ID_Sequencia, sequencia, seqPar, seqElemento, Elemento
  from gElemento 
  order by ID_Sequencia, seqPar;
-- código #4
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 ID_Sequencia, 
       string_agg (Elemento, ',') within group (order by seqPar)
  from gElemento
  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;
Fonte Link
José Dız
  • 7mil
  • 2
  • 18
  • 30
Carregando