String_Aggregation的摘要你好,
我該如何編碼一個表,其中:
-根據字符和
-使用conact_items構建相應的單元格(例如,以分隔)。
-例如SQL代碼“ SELECT sex,String_Agg(Name,',')as ConcatName FROM'Big Class'” GROUP BY sex
我知道可以通過循環來實現,但是我想知道是否還有另一種更優雅的方式。
基於“大類”表,結果應如下所示:
性別ConcatName
f凱蒂,路易絲....
蒂姆(Tim),詹姆斯(James),...
非常感謝
腳本編寫回复:摘要與String_Aggregation列表平台似乎需要連接匯總功能。這是在匯總表的每一行上使用單個循環的一種方法:
Names default to here(1);
dt = Open("$Sample_data/big class.jmp");
//Table with each unique sex and count
dtSum = (dt << Tabulate(
Add Table( Row Table( Grouping Columns( :sex ) ) )
)) << Make Into Data Table;
//blank column for concatenated names
dtSum << New Column("Names", Character, "Nominal");
//loop for each row/sex
for each row(dtSum,
// Use Eval and eval expression to insert the sex of the current
// row into the where expression of the other table
Eval(Eval Expr(
//fill in the names one row at a time, joining all values together
:Names = char( Concat Items(
//get names from main table matching the sex of the summary table
dt:name[ dt << Get Rows Where( dt:sex == Expr(:sex) ) ],
","
) )
))
);
回复:摘要與String_Aggregation這是使用JSL(JMP 15)的嘗試。 也可以使用轉換和合併列以交互方式完成。
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
dt1 = dt << Transpose( columns( :name ), By( :sex ), Output Table( "Test" ) );
dt1 << Combine Columns(
delimiter( ", " ),
Columns( 3 :: N Col( dt1 ) ),
Selected Columns are Indicator Columns( 0 ),
Column Name( "ConcatName" )
) << deletecolumns( 2, 4 :: N Col( dt1 ) );
回复:摘要與String_Aggregation@ih和@小姐已經為您提供了很好的答案,但是我已經在研究此解決方案,因此我想將其發布以展示通過JSL進行此操作的另一種方法。
利用子集在選項子集我們可以為每個子組獲取一個表,然後從“名稱”列中獲取值並使用Concat物品()構建名稱字符串。
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
dt_list = dt << Subset(
By( :sex ),
All rows,
Selected columns only( 0 ),
columns( :name, :age, :height, :weight ),
private
);
nt = New Table();
group_col_name = Word( 1, dt_list[1] << get name, "=" );
nt << New Column( group_col_name, character );
nt << New Column( "concat name" );
nt << add rows( N Items( dt_list ) );
For( i = 1, i <= N Items( dt_list ), i++,
Column( nt, group_col_name )[i] = Word( 2, dt_list[i] << get name, "= " );
nt:concat name[i] = Concat Items( dt_list[i]:name << get values, ", " );
);
For( i = 1, i <= N Items( dt_list ), i++,
Close( dt_list[1], nosave )
);
dt_list = Empty();