1

I have one query, made it in MySQL.

And now try to run on PostgreSQL, but get syntax error twice.

Once on the DIV command, and then on the GROUP BY.

Here is my code:

UPDATE status_table 
SET Status_CG = true
WHERE EXISTS ( SELECT MAX(m2.t1 - m1.t1) as difference 
               FROM measurements AS m1 
               INNER JOIN measurements AS m2 
                  ON `m2`.`Measurements_ID` 
                     > m1.Measurements_ID 
               AND (m2.Measurements_ID -1) DIV 10 
                    = ( m1.Measurements_ID - 1) DIV 10
               GROUP BY (m1.Measurements_ID - 1 ) DIV 10 
               HAVING MAX(m2.t1 - m1.t1) >= 100
              );
3
  • 2
    Please read Help me write this query in SQL and update the question Commented Dec 19, 2022 at 8:11
  • my_sql's DIV predicate can be converted as postgresql / operator
    – Sahap Asci
    Commented Dec 19, 2022 at 9:51
  • SQL uses / for division and so does Postgres. Those dreaded backticks are also invalid standard SQL and invalid in Postgres. Use double quotes " instead.
    – user1822
    Commented Dec 19, 2022 at 11:35

1 Answer 1

0

just doing a direct translation, this would run (three main changes applied):

  • removal of backticks ` `
  • quoting of objects (tables, columns, etc) with uppercase characters "MyName"
  • replacement of DIV with /:
UPDATE status_table
SET "Status_CG" = true
WHERE EXISTS ( SELECT MAX(m2.t1 - m1.t1) as difference
               FROM measurements AS m1
               INNER JOIN measurements AS m2
                  ON m2."Measurements_ID"
                     > m1."Measurements_ID"
               AND (m2."Measurements_ID" -1) / 10
                    = ( m1."Measurements_ID" - 1) / 10
               GROUP BY (m1."Measurements_ID" - 1 ) / 10
               HAVING MAX(m2.t1 - m1.t1) >= 100
              );

If possible, you should consider just using all lowercase names for your tables and columns to avoid the need to quote to preserve uppercase characters in the name. For further info, here is a good post on identifiers and quoting.

Hope this is helpful in illustrating the syntax differences needed for your query to run

1
  • Thank you this is work perfect! Commented Dec 21, 2022 at 5:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.