cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
Lino
Level III

How to Concatenate String over Multiple Rows by Group?

Hello,

 

I have a list of product (> 100) with different product codes of each product. I would like to create new column "Product Code Range" that it concatenates the product code for each product as shown below example. How to write JSL code that can do this task?

 

Product.PNG

4 ACCEPTED SOLUTIONS

Accepted Solutions
jthi
Super User

Re: How to Concatenate String over Multiple Rows by Group?

I have written addin which can help with thisGroup Listagg Columns . It is using sql-query to perform LISTAGG operation. You could install the addin and take code from there. Basic idea is something like this:

Names Default To Here(1);

dt = Current Data Table();	

custom_sql = Eval Insert(
"\[SELECT Product, GROUP_CONCAT(DISTINCT CAST("Product Code" as TEXT)) as "Product Code Range"
FROM t1
GROUP BY Product
]\");

dt_concat = Query(
	Table(dt, "t1"),
	custom_sql
);

jthi_0-1655989397688.png

 

and then join dt_concat back to the original table

 

JSL Syntax Reference > SQL Functions Available for JMP Queries > Aggregate SQL Functions 

-Jarmo

View solution in original post

jthi
Super User

Re: How to Concatenate String over Multiple Rows by Group?

Other option could be to create Associative Array with Products as keys and Product Codes in a list as values and then fill new column with that. This requires JMP16 due to For Each, but it can be replaced with For-loop if necessary

Names Default To Here(1);

dt = Current Data Table();
aa = Associative Array(:Product << get values);

For Each({{key, value}}, aa,
	aa[key] = dt[Loc(dt[0, "Product"], key), "Product Code"];
);

dt << New Column("Product Code Range", Character, Nominal, << Set Each Value(
	Concat Items(aa[:Product], "/");
));
-Jarmo

View solution in original post

jthi
Super User

Re: How to Concatenate String over Multiple Rows by Group?

One more option came to my mind which won't require any scripting (but can be easily scripted/JMP can mostly script it) (note that order will most likely change with this method).

1. Split the original data

jthi_0-1656003074563.png

2. Select all the columns which start by P and use Cols/Combine Columns to create new column

jthi_1-1656003129096.png

3. Join new table with the new combined column to the original data

jthi_2-1656003197830.png

4. Result:

jthi_3-1656003207349.png

 

-Jarmo

View solution in original post

txnelson
Super User

Re: How to Concatenate String over Multiple Rows by Group?

And here is yet another way to do this, using Get Rows Where()

New Column( "PRC",
	character,
	set each value(
		theProduct = :Product[Row()];
		Concat Items(
			:Product Code[Current Data Table() << get rows where( :Product == theProduct )],
			"/"
		);
	)
);

txnelson_0-1656006536451.png

 

Jim

View solution in original post

6 REPLIES 6
jthi
Super User

Re: How to Concatenate String over Multiple Rows by Group?

I have written addin which can help with thisGroup Listagg Columns . It is using sql-query to perform LISTAGG operation. You could install the addin and take code from there. Basic idea is something like this:

Names Default To Here(1);

dt = Current Data Table();	

custom_sql = Eval Insert(
"\[SELECT Product, GROUP_CONCAT(DISTINCT CAST("Product Code" as TEXT)) as "Product Code Range"
FROM t1
GROUP BY Product
]\");

dt_concat = Query(
	Table(dt, "t1"),
	custom_sql
);

jthi_0-1655989397688.png

 

and then join dt_concat back to the original table

 

JSL Syntax Reference > SQL Functions Available for JMP Queries > Aggregate SQL Functions 

-Jarmo
jthi
Super User

Re: How to Concatenate String over Multiple Rows by Group?

Other option could be to create Associative Array with Products as keys and Product Codes in a list as values and then fill new column with that. This requires JMP16 due to For Each, but it can be replaced with For-loop if necessary

Names Default To Here(1);

dt = Current Data Table();
aa = Associative Array(:Product << get values);

For Each({{key, value}}, aa,
	aa[key] = dt[Loc(dt[0, "Product"], key), "Product Code"];
);

dt << New Column("Product Code Range", Character, Nominal, << Set Each Value(
	Concat Items(aa[:Product], "/");
));
-Jarmo
jthi
Super User

Re: How to Concatenate String over Multiple Rows by Group?

One more option came to my mind which won't require any scripting (but can be easily scripted/JMP can mostly script it) (note that order will most likely change with this method).

1. Split the original data

jthi_0-1656003074563.png

2. Select all the columns which start by P and use Cols/Combine Columns to create new column

jthi_1-1656003129096.png

3. Join new table with the new combined column to the original data

jthi_2-1656003197830.png

4. Result:

jthi_3-1656003207349.png

 

-Jarmo
Lino
Level III

Re: How to Concatenate String over Multiple Rows by Group?

Jarmo,

 

Thank you. All solutions provided is great.

It will help me in my learning. 

 

 

txnelson
Super User

Re: How to Concatenate String over Multiple Rows by Group?

And here is yet another way to do this, using Get Rows Where()

New Column( "PRC",
	character,
	set each value(
		theProduct = :Product[Row()];
		Concat Items(
			:Product Code[Current Data Table() << get rows where( :Product == theProduct )],
			"/"
		);
	)
);

txnelson_0-1656006536451.png

 

Jim
Lino
Level III

Re: How to Concatenate String over Multiple Rows by Group?

Jim.

 

It is great. Thank you.

I learn new thing today