A bike shop adds a mechanic column to its service export in February and moves job_id to the right. January has job_id,status. February has status,mechanic,job_id. Load both files by column name and keep the filename so anyone investigating a row can open the original export.
SELECT job_id, status, mechanic, filename
FROM read_csv(['january.csv', 'february.csv'],
union_by_name = true, filename = true,
all_varchar = true, header = true)
ORDER BY filename, job_id;
If January contains 0017,complete and February contains booked,Mara,0018, you should see one row per file. January's mechanic is null. February's mechanic is Mara and its job ID is 0018. Reading columns as text keeps the leading zero. Convert genuine measures in a later query, after you decide how to handle blanks and malformed values.
Reconcile the files before you count jobs
Group the result by filename and compare each count with the source. If job_id is supposed to identify one service job, also group by that ID and investigate repeated values. A second export might contain the same booking after its status changed. Decide whether you want such rows as history or a single current record before using the table to count jobs.
Use a repeated job to check the intended counting rule
Add complete,Mara,0017 to the February fixture. Your grouped result should now show one January row and two February rows. Group by job_id and count rows to confirm that 0017 has two records with different filenames. Keep both rows when the report needs export history. For a report on current jobs, select a record using a verified export date or status timestamp before counting distinct jobs. Keep the chosen row’s filename so a reviewer can trace it to the right file.
