Group By column on SQL Server and concat inner join to that grouped column

Sergio Morera Sanchez

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

GuidoG

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

sql query with grouped by column

GROUP BY to combine/concat a column

concat 2 column values on JOIN sql

SQL Server : group by column with another column

SQL Server : Identity column by group

Sum on a grouped count of a column in SQL Server

SQL Server - Group by - Additional column

Select with column that no in the group by SQL Server

MySQL - LEFT JOIN with GROUP_CONCAT with 3 tables - unknown column

how to concat all column data of a grouped row?

SQL Server: group by a column and get the first group

How to Compare information schema column with Table Column in Sql Server2008 using inner Join?

Adding new column (with inner join) and insert (update) value with case when (SQL Server 2012)

SQL INNER JOIN - Column name in join is also in select *error*

Oracle SQL join three tables and group by column

SQL inner join getting all data in column

How can we Group BY a column in SQL with inner join?

mysql inner join group_concat mysql

Max with inner join need more column in SQL Server

SQL Adding a Column to table, based on a inner join

How to Group By a column - SQL Server

SQL Server INNER JOIN and GROUP BY

find an average of a column using group with inner join and then filtering through the groups

INNER JOIN with a COUNTER COLUMN?

MYSQL query to display max values of column grouped by inner join column

Summing column that is grouped - SQL

SQL - Pivot Column To Rows with Group By and Concat

SQL INNER JOIN Concatenated Field with a column in another table

How to Select a column in the inner join without group by SQL