0

I have two tables in PostgreSQL and I want to join them with where condition. After I joined them, I want to convert to CSV file using copy function. Is it possible to join and generate the CSV file using COPY function? Or is it have another method?

chemat92
  • 491
  • 2
  • 7
  • 15

1 Answers1

1

Yes, it is possible and very easy.

Let's suppose we have two tables, merchant_position and merchant_timeline. In (mp_sc_id, mp_merchant_id, mp_rank, mp_tier, mp_updated_at), all these fields are from the merchant_position table but (mt_name) is in merchant_timeline table and foreign key is mt_id and mp_merchant_id.

\copy (select mp_sc_id, mp_merchant_id, mp_rank, mp_tier, mp_updated_at, mt_name from merchant_position INNER JOIN merchant_timeline ON mt_id = mp_merchant_id) TO '/Users/Desktop/mercahnt_rank.csv' DELIMITER ',' CSV HEADER
Adrian Mole
  • 43,040
  • 110
  • 45
  • 72