updating values of a table from another table through a join changes all values to 1 single value
<p>I have 3 tables: raw_sales, sales and details.  raw_sales is being populated using <code>COPY</code> from a txt file.  All the fields in raw_sales are either string or text. After importing, we run an sql to populate sales and details.  There is a foreign key (sale_id) in details. Here's a sample <code>INSERT</code> command that we use to populate sales and details.</p>

<pre><code>INSERT INTO sales (source, source_identifier)
(SELECT DISTINCT
   'FOO' AS source,
   "identifier" AS source_identifier
FROM raw_sales
LEFT JOIN sales
  ON sales.source_identifier = raw_sales.identifier
 AND sales.source = 'FOO'
WHERE sales.id IS NULL
  AND identifier IS NOT NULL);

INSERT INTO details (sale_id, description)
(SELECT DISTINCT
  sales.id AS sale_id,
  "improvements" as description
FROM raw_sales
JOIN sales
  ON sales.source_identifier = raw_sales.identifier
 AND sales.source = 'FOO'
LEFT JOIN details AS existing
       ON existing.sale_id = sales.id
WHERE existing.id IS NULL
  AND "improvements" != '');
</code></pre>

<p>This seems to work fine.  After this, there's another sql that's being ran to update existing tables.  The query is as follows</p>

<pre><code>UPDATE details SET
  description = "improvements"
FROM raw_sales
JOIN sales
  ON sales.source_identifier = raw_sales.identifier
 AND sales.source = 'FOO'
JOIN details AS existing
  ON existing.sale_id = sales.id
WHERE existing.id IS NOT NULL;
</code></pre>

<p>This query updates all rows in the details table to a single value, the first non-empty value from raw_sales table.  How can I change the above sql so that it updates the existing records in the details table? </p>