-2

So I have this Query that gives me this result: (dont worry about the second ID column)

enter image description here

Is there any way to first Group By "Nombre" but at the same time concat the "Valor" column with the "Nombre" column after it is Grouped?

EXAMPLE:

enter image description here

As this: (Under column "Nombre"): Lista clasificacion : a, b, c

  • 3
    [Please do not upload images of code/errors/data when asking a question.](//meta.stackoverflow.com/q/285551) – Thom A Feb 03 '22 at 15:08
  • As for your question, I actually have no idea what you are asking. You need to elaborate here. – Thom A Feb 03 '22 at 15:09
  • We could help you with that, but unfortunate you have not provided sample data in text but in an image. Do you really expect us to type over all that data ? – GuidoG Feb 03 '22 at 15:12
  • you are right i forgot about the sample data :( gonna fix it – Sergio Morera Sanchez Feb 03 '22 at 15:36
  • Does this answer your question? [Simulating group\_concat MySQL function in Microsoft SQL Server 2005?](https://stackoverflow.com/questions/451415/simulating-group-concat-mysql-function-in-microsoft-sql-server-2005) – Charlieface Feb 03 '22 at 17:25

1 Answers1

1

Without sample data it is hard to tryout, but maybe this can help you

First I create some sample data, but I have no idea if this is correct with your data

declare @Lista table (Id int, Nombre varchar(50))
declare @ListaDetalle table (IdLista int, Valor varchar(10))

insert into @Lista (Id, Nombre) values (1, 'Lista clasfication'), (1, 'Lista clasfication'), (1, 'Lista clasfication'), (7, 'Lista final'), (7, 'Lista final')
insert into @ListaDetalle (IdLista, Valor) values (1, 'a'), (1, 'b'), (1, 'c'), (7, 'Valor 01'), (7, 'Valor 02')

Now we can use this query which returns what you asked in your question

select l.Nombre + ': ' +
       ( select string_agg(ld2.Valor, ',')
         from   @ListaDetalle ld2
         where  ld2.IdLista = l.Id
       ) as Nombre 
from   @Lista l
  inner join @ListaDetalle ld on l.Id = ld.IdLista
group by l.Id, l.Nombre

The result is

Lista clasfication: a,b,c
Lista final: Valor 01,Valor 02
GuidoG
  • 11,359
  • 6
  • 44
  • 79