csvq icon indicating copy to clipboard operation
csvq copied to clipboard

Column update from one Table to another based on a ID match

Open ahmedmohiduet opened this issue 2 years ago • 2 comments

How can I Column update from one Table to another based on a ID match?

I have one equery: csvq 'update nullbatches join file on nullbatches.Inv_date=file.Inv_date set nullbatches.Btc_no = file.Btc_no where file.P_code!=9 and file.P_code!=42 and nullbatches.P_code!=9 and nullbatches.P_code!=42' ( my csvs: nullbatches.csv, file.csv)

Which says: [L:1 C:20] syntax error: unexpected token "join"

Is it doable?

ahmedmohiduet avatar Aug 10 '23 04:08 ahmedmohiduet

I had a similar issue recently and got around it by something like

UPDATE nullbatches
SET nullbatches.Btc_no = (
   SELECT file.Btc_no 
   FROM file 
   WHERE nullbatches.Inv_date = file.Inv_date
)

ondohotola avatar Aug 10 '23 10:08 ondohotola

You can also write the query like this.

UPDATE n
   SET n.Btc_no = f.Btc_no
  FROM nullbatches n
       JOIN file f
         ON f.Inv_date = n.Inv_date
 WHERE NOT f.P_code IN (9, 42)
   AND NOT n.P_code IN (9, 42);

cf. https://mithrandie.github.io/csvq/reference/update-query.html

mithrandie avatar Aug 13 '23 17:08 mithrandie