Query multiple selected row from different tables

  • Goal: <!--- I am trying to create a table that as source has the selected rows (multiple) for different tables of the same dataset.

  • Details:

So I know that this for single row for each table works

However does not work for multiple rows with the SelectedSourceRows.

When I try to create separate function to combine all the elements before with this JS code and this SQL query

SQL

SELECT * FROM pineberry WHERE site IN ({{ formatted_sites.data }})

JS

function extractSites(table) {
if (!table.selectedSourceRows || table.selectedSourceRows.length === 0) {
return ''; // Return an empty string if there are no selected rows
}
// Collect site names from selected rows
return table.selectedSourceRows.map(row => row.site ? "${row.site}" : null)
.filter(site => site).join(', ');
}

// Aggregate sites from all relevant tables
const sitesArray = [
extractSites(table_sweden),
extractSites(table_norway),
extractSites(table_denmark),
extractSites(table_finland),
extractSites(table_germany)
].filter(s => s.length > 0).join(', ');

// Format for SQL usage: without extra quotes
const formattedSites = sitesArray.length > 0 ? sitesArray : "''"; // Ensure SQL safe empty default

// Output the formatted sites string for SQL
return formattedSites;

the output is the following, which does not work since I believe it adds the "" for the string

So I was wondering is there a way to do everything from the SQL query with SelectedSourcesRow in stead of SelectedSourceRow or I need to do the JS in a different way to output the correct format?

Also from the Retool documentation it says that I should handle the array with "@". So I tried this, but does not work.

Thank you!

Hi @darl3190

Thanks for reaching out! You should be able to use the Postgres syntax from this post SQL Cheatsheet - best practices for different flavors of SQL syntax