How to concatenate the results of a RELATION into a SELECT?

Asked

Viewed 8,408 times

2

Having the tables PRODUTO , CATEGORIA whereas a CATEGORY may have several PRODUCTS, would like a select list in a column nvarchar all the PRODUCT NAME of a CATEGORY.

Something like:

       select concat( select p.nome from categoria c 
       inner join produto p on c.id = p.fk_categoria 
       where c.id = categoria.id) as ProdutosDaCategoria from categoria

inserir a descrição da imagem aqui

This code won’t work but it’s an attempt to explain what I need.

You would also have to put a "," or other separator character between each result of subquery.

Someone knows how to do ?

Note: any solution that brings all the categories listed and the "name" of products concatenates in an nvarchar meets the problem (as long as the query is not unviable expensive ) .

2 answers

1


Here’s a solution using FOR XML with PATH mode

SELECT DISTINCT C.ID, 
       SUBSTRING((
          SELECT ', ' + P.NOME
          FROM dbo.Produtos P
          WHERE P.FK_CATEGORIA = C.ID
          FOR XML PATH ('')
        ), 2, 1000) TODOS_OS_PRODUTOS
FROM dbo.Categorias C
ORDER BY C.ID

Stay here the Sqlfiddle

  • 1

    Gave straight !!! Thank you very much !!!!!!!

  • 1

    You’re welcome, I’m glad you helped.

  • Very cool ! I didn’t know that in for xml path if you put + 'algumastring' it stopped displaying the tags to display it in place , this will help a lot !!

0

without the database structure gets a little complicated to help you, but anyway try to accomplish this way.

select processo.numero , Concat(descricao_parcela.nome,', ', outro_campo) as objetosDaAcao 
  from processo
  left join parcela 
    on >>>> veja as ligações necessarias <<<<
  left join descricao_parcela 
    on descricao_parcela.id = parcela.fk_descricao_parcela 
 where processo.id = parcela.fk_processo 
 order by processo.id 

in the Concat you define the form and the fields you will need if they will be separated by point, comma, quotes and so on, just pass the function with single quotes and separate by comma all fields and characters that will be added.

See the link to the parcel table in more case it doesn’t work send a message that I help you !! Hug...

  • sorry , I made a change in the post so that it had only 2 tables and was simpler . I will try your solution now and I already bring you feedback ! In the case does not need the structure of them unless knowing the FKS , being that a process has an id , parcel has fk_process and fk_descricao_parcel and descricao_parcel has id. In the example I modified I take into consideration that a product has an fk_category and category an id . the example should serve to concatenate any field

  • Your example won’t work... because I don’t want to display a result of a relationship but ALL the results concatenated and it would only work when a category had only one product. Anyway thank you so much for trying to help !

  • 1

    The Concat function was only available in SQL Server 2012

  • yes but for the version 2005 or 2008 onwards using (campoX, ', ',Campoy) the columnsX it already interprets and performs the concatenation, I had forgotten it, I’m used to the 2014

Browser other questions tagged

You are not signed in. Login or sign up in order to post.