1
I have a table that stores the EST_PROD_CD_BARA barcodes of the products, however, I would like to bring all the Eans of an item in just one line based on the id (cd_prod), since, a product can have n barcodes.
While searching I found that a way to return the various results in only one line could be using PIVOT (Sqlserver resource), I tried to apply but could not successfully perform. Could someone help me?
declare @colunas_pivot as nvarchar(max), @comando_sql as nvarchar(max)
set @colunas_pivot =
stuff((
select
distinct ',' + quotename(CD_PROD)
from EST_PROD_CD_BARRA
for xml path('')
), 1, 1, '')
set @comando_sql = '
select * from (
SELECT
B.CD_PROD,
B.TP_CD_BARRA,
B.CD_BARRA
FROM EST_PROD_CD_BARRA B
GROUP BY B.CD_PROD,B.CD_BARRA
) em_linha
pivot (MAX(CD_BARRA) for CD_PROD in (' + @colunas_pivot + ')) em_colunas
order by 1'
print @comando_sql
execute(@comando_sql)
go
What result does it give you? You can put an example of the result you expect?
– João Martins