195
Views
12
Comments
Solved
How to concatenate all the rows linked with a same id into one row?
Question

How to concatenate all the rows linked with a same id into one row?

UserImage.jpg
vikas sharma
Champion
Solution
2021-06-09 13-39-18
Agno Silveira
 
MVP

Hello Ernesto,

Could you give us more details of your demand?

Maybe this function can help you:

Regards.

UserImage.jpg
Ernesto De La Pena Molina

2018-08-27 08-29-35
Fábio Vaz

Hello Ernesto,

Sorry but you need detail more your question... 

What kinda of rows, DB, excel?

into on row? 

I really like to help but need more information

UserImage.jpg
Ernesto De La Pena Molina


UserImage.jpg
vikas sharma
Champion

Hi,

As you tagged the "SQL" & "Aggregate" in question, so I am assuming you are asking for the database result from aggregate or SQL.

So basically your case is for one to many relationship. There may be multiple rows connected with one Id and in data base query you want that all rows should be there in one column separated by some delimiter.  For this you need to use SQL query with STUFF() keyword to get the multiple rows in a single row.

https://www.mytecbits.com/microsoft/sql-server/concatenate-multiple-rows-into-single-string

if this is not the case then please let us know exact use case of the issue.

regards

UserImage.jpg
Ernesto De La Pena Molina

I tried those solutions but did not work in Outsystems 

UserImage.jpg
Ernesto De La Pena Molina

Please see the pic I want to combine multiple rows of values with the same ID in SQL or aggregates , to produce a table. 

Capture.PNG
UserImage.jpg
vikas sharma
Champion

As I already answered you need to use SQL query with STUFF keyword here and it works in OutSystems perfectly. I have done this many times. if facing issue then please share OML.

regards

UserImage.jpg
Ernesto De La Pena Molina


SELECT {LogisticBlockFreshWarehouse}.[LogisticBlockFreshId],STUFF((SELECT CONCAT(', ', {LogisticBlockFreshWarehouse}.[WarehouseId])                         FROM {LogisticBlockFreshWarehouse} i                        WHERE i.[LogisticBlockFreshId] = o.[LogisticBlockFreshId]                FOR XML PATH ('')),1,1,'') AS {LogisticBlockFreshWarehouse}.[WarehouseId])               FROM {LogisticBlockFreshWarehouse} o GROUP BY {LogisticBlockFreshWarehouse}.[LogisticBlockFreshId]


UserImage.jpg
vikas sharma
Champion
Solution

Hi,

Please check below example, I created for you.


UserImage.jpg
Ernesto De La Pena Molina

Thank you for providing that information quickly. When  I remove the comma  it is working but if I keep the comma I got this error. 

UserImage.jpg
Ernesto De La Pena Molina

I added 

 CAST ( pp.[WarehouseId] AS VARCHAR(255) it is working. Thank you

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.