title: Postgres query returning SQL Error syntax error at "Where"
body: <p>I have the following query, when I execute it I'm getting an error </p>
  
  <pre><code>INSERT INTO AM_EVENT_STATUS (EVENT_STATUS_ID, EVENT_ID, TIMESTAMP,OLD_STATUS_ID, NEW_STATUS_ID, NOTE, USERNAME)
  SELECT
  "external_id" AS EVENT_STATUS_ID,
  "external_id" AS EVENT_ID,
  "timestamp"   AS TIMESTAMP,
  NULL          AS OLD_STATUS_ID,
  'unknown'     AS NEW_STATUS_ID,
  NULL          AS NOTE,
  NULL          AS USERNAME
  FROM (SELECT "2cba77463c899fee1656b367e39892"
    FROM "2cba77463c899fee1656b367e39892"
    INNER JOIN (SELECT "external_id", MAX("timestamp") AS "timestamp"
              FROM "2cba77463c899fee1656b367e39892"
                GROUP BY "external_id") LATEST WHERE "2cba77463c899fee1656b367e39892"."external_id" = LATEST."external_id"
      AND "2cba77463c899fee1656b367e39892"."timestamp" = LATEST."timestamp"
      AND "2cba77463c899fee1656b367e39892"."event_type" = 'transaction_update');
  </code></pre>
  
  <p>Error is:</p>
  
  <blockquote>
    <p>SQL Error [42601]: ERROR: syntax error at or near "WHERE"   Position:
    601   org.postgresql.util.PSQLException: ERROR: syntax error at or
    near "WHERE"   Position: 601</p>
  </blockquote>
  
  <p>What am I doing wrong here ? Thanks in advance.</p>