Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Concatenate multiple rows in one column with same id

AJeB
Participant
0 Kudos
4,729

hello.

how can i concatenate the tmdocs field if the sales id are same
i create a internal program for this but i don't have any idea what to do next, what syntax i need to use or if there is a function module for this or class.

output should be something like this

1 ACCEPTED SOLUTION

former_member808116
Participant
0 Kudos
3,783

Note: It1 as internal table Img1. It2 as Img2.

Data: wa2 like line of it2,
      Lv_tr type string.

Sort It1 by salesid.
Loop at It1 into data(wa1).
If lv_str is initial.
Concatenate lv_str wa1-salesid into lv_str.
Else.
Concatenate lv_str wa1-salesid into lv_str special by ','.
Endif.

At end salesid.
Wa2-saleid = wa1-saleid.
Wa2-tmdocs = lv_str.
Append wa2 to it2.
Clear: wa2, lv_str.
Endat.
Clear wa1-salesid.
Endloop.
11 REPLIES 11

former_member808116
Participant
0 Kudos
3,784

Note: It1 as internal table Img1. It2 as Img2.

Data: wa2 like line of it2,
      Lv_tr type string.

Sort It1 by salesid.
Loop at It1 into data(wa1).
If lv_str is initial.
Concatenate lv_str wa1-salesid into lv_str.
Else.
Concatenate lv_str wa1-salesid into lv_str special by ','.
Endif.

At end salesid.
Wa2-saleid = wa1-saleid.
Wa2-tmdocs = lv_str.
Append wa2 to it2.
Clear: wa2, lv_str.
Endat.
Clear wa1-salesid.
Endloop.

0 Kudos
3,783

"special by ','."

Will not work buddy. You want to use "separate by" I guess.

Best,

Edrilan Berisha

SAP S/4HANA Financials Development


0 Kudos
3,783

thanks for this. but it gives me wrong output it concatenated but the value of the second column with salesid of 8100000455 is this 8100000455,8100000455,8100000455,8100000455,8100000455,8100000455,8100000455,8100000455,8100000455,8100000455

0 Kudos
3,783

i changed the logic and it works thanks. it concatenate the sales id not the tmdocs

0 Kudos
3,783

just a quick question is this possible in CDS View?

0 Kudos
3,783

yes it can be done using CDS with table functions and usethe STRING_AGGR function

0 Kudos
3,783

LOOP AT GROUP BY would be a more modern and relevant syntax to get this done.

Edrilan_Berisha
Product and Topic Expert
Product and Topic Expert
0 Kudos
3,783

Hi,

let's say the first table from image1 is an internal table called lt_sales. And you want to update the database table in the end. Let's assume the database table is named sales_db_table. The logic is like this:

LOOP AT lt_sales INTO DATA(ls_sales) GROUP BY ls_sales-salesid.
  LOOP AT GROUP ls_sales INTO DATA(ls_sales_grouped).
    IF lv_conc_string IS INITIAL.
      lv_conc_string = ls_sales_grouped-tmdocs
    ELSE.
      CONCATENATE lv_conc_string ls_sales_grouped-tmdocs INTO lv_conc_string SEPARATED BY ','.
    ENDIF.
  ENDLOOP.
"now do something with the concatenated string, modify the table where you want to change the tmdocs for instance
 UPDATE sales_db_table SET tmdocs = @lv_conc_string WHERE salesid = @ls_sales-salesid.
ENDLOOP.

Sandra_Rossi
Active Contributor
3,783
DATA(string_table) = VALUE string_table(
( `8100000455` ) ( `8100000151` ) ( `8100004000` ) ( `8100000456` ) ). DATA(field) = concat_lines_of( sep = `, ` table = string_table ). ASSERT field = `8100000455, 8100000151, 8100004000, 8100000456`.

ThorstenHoefer
Active Contributor
3,783

Hi,

try to use a group by like:

https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abaploop_at_group.htm

type: begin of ty_list,
	    no  type n length 6,
	   docs type string,	  
	  end of ty_list.
	  
data: lt_list type table of ty_list.	  

DATA(list) = VALUE list( ).
LOOP AT lt_data INTO DATA(wa)
     GROUP BY wa-no
     ASCENDING
     ASSIGNING FIELD-SYMBOL(<list>).

  lt_list = VALUE ty_list(
           BASE lt_list
           ( no = <list>
             docs = concat_lines_of( 
                       sep = `, ` 
                       table = value #( FOR wa IN GROUP <list> ( wa-doc ) ) 
                    )
          ) 
        ).
ENDLOOP.

shivapatil2021
Discoverer
0 Kudos
3,783

Hi,

Same thing i want to write in BW END ROUTINE.

Please provide the updated code.