Hey there @jose_viera, and welcome to the forum.
There are several ways you can achieve this, depending on how you want to use your data. I asked Chatgpt to provide 3 options, SQL to combine the queries or JAvascript/Lodash to merge them.
These are things that ChatGPT can provide you really cool solutions. Let me know if you need any further input.
Cheers!
SQL Approach (Using JOIN & SUM)
If both tables exist in the same database, you can join them using LEFT JOIN
and aggregate the payments using SUM
.
sql
CopyEdit
SELECT
f.id,
f.name,
COALESCE(SUM(p.amount), 0) AS total_payments
FROM freelancers f
LEFT JOIN payments p ON f.id = p.freelancer_id
GROUP BY f.id, f.name
ORDER BY f.id;
LEFT JOIN
ensures all freelancers are included, even if they have no payments.
SUM(p.amount)
calculates the total payments per freelancer.
COALESCE(SUM(p.amount), 0)
ensures that freelancers with no payments show 0
instead of NULL
.
GROUP BY f.id, f.name
groups data by freelancer.
JavaScript Approach (Using Lodash or Vanilla JS)
If getFreelancers.data
and getPayments.data
exist separately in Retool, you can merge them using JavaScript.
Vanilla JavaScript Solution
javascript
CopyEdit
const freelancers = getFreelancers.data;
const payments = getPayments.data;
// Create a mapping of freelancer payments
const paymentMap = payments.reduce((acc, { freelancer_id, amount }) => {
acc[freelancer_id] = (acc[freelancer_id] || 0) + amount;
return acc;
}, {});
// Merge with freelancers
const result = freelancers.map(f => ({
id: f.id,
name: f.name,
total_payments: paymentMap[f.id] || 0
}));
result;
Lodash Approach
If you have Lodash in Retool, you can use _.groupBy
and _.sumBy
for a more declarative solution:
javascript
CopyEdit
const freelancers = getFreelancers.data;
const payments = getPayments.data;
// Group payments by freelancer_id
const groupedPayments = _.groupBy(payments, "freelancer_id");
// Map freelancers and sum their payments
const result = freelancers.map(f => ({
id: f.id,
name: f.name,
total_payments: _.sumBy(groupedPayments[f.id] || [], "amount") || 0
}));
result;
Which One Should You Use?
- Use the SQL query if both datasets come from the same database → it's more efficient.
- Use the JavaScript method if
getFreelancers
and getPayments
are separate queries → Retool transformers can handle this easily.
Let me know if you need adjustments! 