Combinazione di dati da più tabelleSto cercando di creare una nuova tabella principale combinando i dati di due tabelle (o più). Ad esempio, sto cercando di combinare la tabella A e la tabella B di seguito nella tabella AB come risultato:



Fondamentalmente entrambe le tabelle A e B hanno colonne simili in "inizio" e "fine" ma un intervallo diverso per ogni riga. Quello che sto cercando di fare è combinare i segmenti dalla distanza iniziale 1 a 20 da entrambe le tabelle e combinarli nella tabella AB.
Qualche idea su come eseguire questa operazione tramite lo scripting o le funzioni Tabelle?
Tabella datiScriptingRi: Combinazione di dati da più tabelleQuesto può dare un'idea di come potresti farlo con lo scripting:
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 << get rows where(startVal <= :Start <= endVal | startVal <= :End <= endVal);
For(i = 1, i <= 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 << 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);Ri: Combinazione di dati da più tabellePuoi anche affrontarlo con la versione di JSL di una tabella di ricerca, usando Loc Sorted ().
Saluti,
Brady
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 << get values ), As List( dt2:End << get values ) ) ) << get keys );
starts = [1] |/ ends[1 :: (N Row( ends ) - 1)];
dt3 = As Table( starts || ends, <<COLUMN names=""></COLUMN>Ri: Combinazione di dati da più tabelle@jthi & @brady_brady Grazie per le risposte. Sono sicuro che le soluzioni fornite funzionano, ma voglio prima digerirle e risponderò qui se soddisfano il mio intento. Apprezzo l'aiuto! Tenervi aggiornati.
Ri: Combinazione di dati da più tabelleDopo un po 'di riflessione, ho trovato un modo per farlo in modo interattivo. Piuttosto che descriverlo, ho allegato un breve video che ho realizzato che discute il processo. L'approccio interattivo ha il vantaggio che è la stessa quantità di lavoro (che non è molto) indipendentemente dal numero di tabelle che si desidera combinare. Nel mio esempio, utilizzo 3. Lo svantaggio è che richiede uno smistamento non intuitivo; se commetti un errore nell'ordinamento, otterrai risultati errati.
Esegui questo script per generare le tabelle che uso nell'esempio:
Names Default To Here(1);
dta = new table("A",
<<ADDROWS></ADDROWS>