Union is the wrong operation, it would add columns, so your final table would have all columns 3 times with suffixes automatically added to avoid double names.
You want to join data and sum grouped by all fields, mainly account_name, but all fields that you don't SUM. Join only appends data, but group then makes 1 out of all three records from jan,feb,mar.
Aside of that, having the filenames in data is fine, you only use one of the three tables, though. It's not necessary to use dbfs before SQL, SQL can also open the tables. So all you'll need is CD into the folder of them, unless they are all in different folders. If each month is a folder, then it would make sense to first use them, but then do it with a scan-loop on curTemp:
Code:
Select curTemp && btw is unnecessary directly after a query INTO CURSOR curTemp, curTemp is the currently activated workarea.
Scan
USE (alltrim(curTemp.name)) IN 0 SHARED
Endscan
the query should be something like
Code:
SELECT fieldlist, SUM(TAX_BASE) AS TAX_BASE,SUM(AMT_WTH) AS AMT_WTH;
FROM REMITTANCE_JAN2022 jan;
INNER JOIN REMITTANCE_FEB2022 feb ON jan.account_name = feb.account_name;
INNER JOIN REMITTANCE_MAR2022 mar ON jan.account_name = mar.account_name;
GROUP BY fieldlist;
INTO CURSOR curDtl READWRITE
I don't know your data, but this might fail on too many fields. If you store say a transaction number, lets call it tn just for sake of exposition to talk about it. If that tn differs for each record, you can't have all of these numbers aggregated into one record that totals jan-mar. This field has to be dropped.
More concretely said if for account_name AAA there is a jan transaction with number 1, a feb transaction with number 3243 and a mar transaction with number 546456 you cant get a list 1,3243,546456 with a GROUP BY aggregation, you could sum them, which gives a nonsense result as that transaction number sum does not exist as transaction number, you could take the lowest of them with MIN(tn) or the highest with MAX(tn), but you'd need to create three columns tn_jan, tn_feb, tn_mar to store them all.
And what is said for a transaction number may be true for many of your fields. This will become a more complicate operation than just one group by query, if you need such lists or multiple fields to store values of jan,feb, and mar separately. The simpler solution is to drop the fields, if you still need to display them all, that's done with a join of the totaled record with the jan,feb, mar detail records that you still have, and that's how that's usually done instead of generating this into new fields.
And besides that "bad news" for all non-grouped fields, the problem becomes worse, if you have multiple records in each month already. Then you could need more fields for the single vaules, in theory even multiple per day. So that's surely not something you'd design, you'll need to drop this info from the total result as it's details that are all still available in the jan.mar tables and can be taken from there, when necessary.
Chriss