0

I have the following query, when I execute it I'm getting an error

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');

Error is:

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

What am I doing wrong here ? Thanks in advance.

4
  • Please do not use tags that do not apply to your question Commented Jan 25, 2017 at 2:05
  • missing AS: ...) AS LATEST WHERE ... Commented Jan 25, 2017 at 2:15
  • tried that didn't work Commented Jan 25, 2017 at 2:17
  • John, make sure you explain why adding the correct tag is important instead of dictating something for no reason. It's extremely unprofessional and makes the community look bad. Commented May 1, 2021 at 19:15

2 Answers 2

2

Change the LATEST WHERE to LATEST ON. You have a JOIN, so the correct comparison clause is ON, not WHERE.

You also need a table alias after the last parentheses.

Sign up to request clarification or add additional context in comments.

2 Comments

nope didn't work. SQL Error [42601]: ERROR: subquery in FROM must have an alias Hint: For example, FROM (SELECT ...) [AS] foo. Position: 344 org.postgresql.util.PSQLException: ERROR: subquery in FROM must have an alias Hint: For example, FROM (SELECT ...) [AS] foo. Position: 344
quick question, i'm trying to select "units_sold" field which is nested inside a jsonb field "custom_fields". How can I access that field ?
0
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") AS LATEST 
    ON "2cba77463c899fee1656b367e39892"."external_id" = LATEST."external_id"
    AND "2cba77463c899fee1656b367e39892"."timestamp" = LATEST."timestamp"
    AND "2cba77463c899fee1656b367e39892"."event_type" = 'transaction_update') as EVENTSTATUS;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.