<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Combining Data from multiple tables in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Combining-Data-from-multiple-tables/m-p/383091#M63374</link>
    <description>&lt;P&gt;EDIT: I re-wrote this to make it much shorter, more efficient and free of the integer-based limitations of my original response. It is also straightforward to adapt the code to handle more than 2 source tables.&lt;/P&gt;
&lt;P&gt;////&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also tackle this with JSL's version of a table-lookup, using Loc Sorted().&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;Brady&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );

dt1 = New Table( "Table A",
	Add Rows( 4 ),
	Compress File When Saved( 1 ),
	New Column( "Start", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 5, 10, 15] ) ),
	New Column( "End", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [5, 10, 15, 20] ) ),
	New Column( "Name", Character, "Nominal", Set Values( {"A", "B", "C", "D"} ) )
);

dt2 = New Table( "Table B",
	Add Rows( 5 ),
	Compress File When Saved( 1 ),
	New Column( "Start", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 4, 8, 12, 16] ) ),
	New Column( "End", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [4, 8, 12, 16, 20] ) ),
	New Column( "Name", Character, "Nominal", Set Values( {"X1", "X2", "X3", "X4", "X5"} ) )
);

//////////// Begin processing here...
ends = Matrix( Associative Array( Concat( As List( dt1:End &amp;lt;&amp;lt; get values ), As List( dt2:End &amp;lt;&amp;lt; get values ) ) ) &amp;lt;&amp;lt; get keys );
starts = [1] |/ ends[1 :: (N Row( ends ) - 1)];
dt3 = As Table( starts || ends, &amp;lt;&amp;lt;column names( {"Start", "End"} ) );
dt1Starts = dt1:Start &amp;lt;&amp;lt; get values;
dt2Starts = dt2:Start &amp;lt;&amp;lt; get values;
dt3 &amp;lt;&amp;lt; New Column( "Name ID",
	formula( dt1:Name[Loc Sorted( dt1Starts, :Start )][1] || " " || dt2:Name[Loc Sorted( dt2Starts, :Start )][1] )
);
dt3:Name ID &amp;lt;&amp;lt; Delete Formula;&lt;BR /&gt;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 07 May 2021 05:05:16 GMT</pubDate>
    <dc:creator>brady_brady</dc:creator>
    <dc:date>2021-05-07T05:05:16Z</dc:date>
    <item>
      <title>Combining Data from multiple tables</title>
      <link>https://community.jmp.com/t5/Discussions/Combining-Data-from-multiple-tables/m-p/382674#M63337</link>
      <description>&lt;P&gt;I'm trying to create a new master table by combining data from two tables (or more). As an example, I'm trying to combine Table A &amp;amp; Table B below into Table A-B as the result:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Djtjhin_0-1620273011098.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/32632i547640C24D6D88A6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Djtjhin_0-1620273011098.png" alt="Djtjhin_0-1620273011098.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Djtjhin_1-1620273026277.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/32633iE36D751BFC76C011/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Djtjhin_1-1620273026277.png" alt="Djtjhin_1-1620273026277.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Djtjhin_2-1620273044453.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/32634i26161962E448861B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Djtjhin_2-1620273044453.png" alt="Djtjhin_2-1620273044453.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Basically both table A &amp;amp; B have similar column in "start" &amp;amp; "end" but different range for each row. What I'm trying to do is to combine the segments from starting distance 1 to 20 from both table and combine them into table A-B.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any idea on how to do this through either scripting or the Tables functions ?&lt;/P&gt;</description>
      <pubDate>Sat, 10 Jun 2023 23:29:53 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Combining-Data-from-multiple-tables/m-p/382674#M63337</guid>
      <dc:creator>Djtjhin</dc:creator>
      <dc:date>2023-06-10T23:29:53Z</dc:date>
    </item>
    <item>
      <title>Re: Combining Data from multiple tables</title>
      <link>https://community.jmp.com/t5/Discussions/Combining-Data-from-multiple-tables/m-p/382705#M63339</link>
      <description>&lt;P&gt;This can give an idea how you could do this with scripting:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

dt1 = New Table("Table A",
	Add Rows(4),
	Compress File When Saved(1),
	New Column("Start",
		Numeric,
		"Continuous",
		Format("Best", 12),
		Set Values([1, 5, 10, 15])
	),
	New Column("End",
		Numeric,
		"Continuous",
		Format("Best", 12),
		Set Values([5, 10, 15, 20])
	),
	New Column("Name", Character, "Nominal", Set Values({"A", "B", "C", "D"}))
);

dt2 = New Table("Table B",
	Add Rows(5),
	Compress File When Saved(1),
	New Column("Start",
		Numeric,
		"Continuous",
		Format("Best", 12),
		Set Values([1, 4, 8, 12, 16])
	),
	New Column("End",
		Numeric,
		"Continuous",
		Format("Best", 12),
		Set Values([4, 8, 12, 16, 20])
	),
	New Column("Name",
		Character,
		"Nominal",
		Set Values({"X1", "X2", "X3", "X4", "X5"})
	)
);

//create collection table
dt_final = New Table("Table A-B",
	Compress File When Saved(1),
	New Column("Start", Numeric, "Continuous", Format("Best", 12)),
	New Column("End", Numeric, "Continuous", Format("Best", 12)),
	New Column("Name", Character, "Nominal")
);

For Each Row(dt1,
	startVal = Column(dt1, "Start")[Row()];
	endVal = Column(dt1, "End")[Row()];
	nameVal = Column(dt1, "Name")[Row()];
	r_dt2 = dt2 &amp;lt;&amp;lt; get rows where(startVal &amp;lt;= :Start &amp;lt;= endVal | startVal &amp;lt;= :End &amp;lt;= endVal);
	For(i = 1, i &amp;lt;= N Items(r_dt2), i++,
		newEnd = Min(Column(dt2, "End")[r_dt2[i]], endVal);
		newName = nameVal || " " ||Column(dt2, "Name")[r_dt2[i]];
		newRow = EvalList({startVal, newEnd, newName});
		dt_final &amp;lt;&amp;lt; Add rows({Start = newRow[1], End = newRow[2], Name = newRow[3]});
		startVal = newEnd;
		endVal= Max(Column(dt2, "End")[r_dt2[i]], endVal);
	);
);

Close(dt1, no save);
Close(dt2, no save);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 May 2021 06:30:04 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Combining-Data-from-multiple-tables/m-p/382705#M63339</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2021-05-06T06:30:04Z</dc:date>
    </item>
    <item>
      <title>Re: Combining Data from multiple tables</title>
      <link>https://community.jmp.com/t5/Discussions/Combining-Data-from-multiple-tables/m-p/383091#M63374</link>
      <description>&lt;P&gt;EDIT: I re-wrote this to make it much shorter, more efficient and free of the integer-based limitations of my original response. It is also straightforward to adapt the code to handle more than 2 source tables.&lt;/P&gt;
&lt;P&gt;////&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also tackle this with JSL's version of a table-lookup, using Loc Sorted().&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;Brady&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );

dt1 = New Table( "Table A",
	Add Rows( 4 ),
	Compress File When Saved( 1 ),
	New Column( "Start", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 5, 10, 15] ) ),
	New Column( "End", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [5, 10, 15, 20] ) ),
	New Column( "Name", Character, "Nominal", Set Values( {"A", "B", "C", "D"} ) )
);

dt2 = New Table( "Table B",
	Add Rows( 5 ),
	Compress File When Saved( 1 ),
	New Column( "Start", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [1, 4, 8, 12, 16] ) ),
	New Column( "End", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [4, 8, 12, 16, 20] ) ),
	New Column( "Name", Character, "Nominal", Set Values( {"X1", "X2", "X3", "X4", "X5"} ) )
);

//////////// Begin processing here...
ends = Matrix( Associative Array( Concat( As List( dt1:End &amp;lt;&amp;lt; get values ), As List( dt2:End &amp;lt;&amp;lt; get values ) ) ) &amp;lt;&amp;lt; get keys );
starts = [1] |/ ends[1 :: (N Row( ends ) - 1)];
dt3 = As Table( starts || ends, &amp;lt;&amp;lt;column names( {"Start", "End"} ) );
dt1Starts = dt1:Start &amp;lt;&amp;lt; get values;
dt2Starts = dt2:Start &amp;lt;&amp;lt; get values;
dt3 &amp;lt;&amp;lt; New Column( "Name ID",
	formula( dt1:Name[Loc Sorted( dt1Starts, :Start )][1] || " " || dt2:Name[Loc Sorted( dt2Starts, :Start )][1] )
);
dt3:Name ID &amp;lt;&amp;lt; Delete Formula;&lt;BR /&gt;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 May 2021 05:05:16 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Combining-Data-from-multiple-tables/m-p/383091#M63374</guid>
      <dc:creator>brady_brady</dc:creator>
      <dc:date>2021-05-07T05:05:16Z</dc:date>
    </item>
    <item>
      <title>Re: Combining Data from multiple tables</title>
      <link>https://community.jmp.com/t5/Discussions/Combining-Data-from-multiple-tables/m-p/383094#M63375</link>
      <description>&lt;P&gt;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/14366"&gt;@jthi&lt;/a&gt;&amp;nbsp;&amp;amp;&amp;nbsp;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/3552"&gt;@brady_brady&lt;/a&gt;&amp;nbsp;Thanks for the responses. I'm sure the solutions provided work but I want to digest them first and I'll respond back here if they meet my intent. Appreciate the help! Keep you posted.&lt;/P&gt;</description>
      <pubDate>Fri, 07 May 2021 06:44:44 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Combining-Data-from-multiple-tables/m-p/383094#M63375</guid>
      <dc:creator>Djtjhin</dc:creator>
      <dc:date>2021-05-07T06:44:44Z</dc:date>
    </item>
    <item>
      <title>Re: Combining Data from multiple tables</title>
      <link>https://community.jmp.com/t5/Discussions/Combining-Data-from-multiple-tables/m-p/383182#M63380</link>
      <description>&lt;P&gt;After some thinking, I found a way to do this interactively. Rather than describe it, I've attached a short video I made that discusses the process. The interactive approach has the advantage that it is the same amount of work (which is not much) no matter how many tables you want to combine. In my example, I use 3. The disadvantage is that it requires some non-intuitive sorting; if you make an error in sorting, you'll get incorrect results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Run this script to generate the tables I use in the example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

dta = new table("A",
          &amp;lt;&amp;lt;addrows(6),
          &amp;lt;&amp;lt;new column("Start", formula(8*(row()-1))),
          &amp;lt;&amp;lt;new column("End", formula(:Start + 8)),
          &amp;lt;&amp;lt;new column("A", character, formula("A" || char(row())))
);
dtb = new table("B",
          &amp;lt;&amp;lt;addrows(12),
          &amp;lt;&amp;lt;new column("Start", formula(4*(row()-1))),
          &amp;lt;&amp;lt;new column("End", formula(:Start + 4)),
          &amp;lt;&amp;lt;new column("B", character, formula("B" || char(row())))
);
dtc = new table("C",
          &amp;lt;&amp;lt;addrows(8),
          &amp;lt;&amp;lt;new column("Start", formula(6*(row()-1))),
          &amp;lt;&amp;lt;new column("End", formula(:Start + 6)),
          &amp;lt;&amp;lt;new column("C", character, formula("C" || char(row())))
);
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 May 2021 13:47:40 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Combining-Data-from-multiple-tables/m-p/383182#M63380</guid>
      <dc:creator>brady_brady</dc:creator>
      <dc:date>2021-05-07T13:47:40Z</dc:date>
    </item>
  </channel>
</rss>

