Skip to content
Advertisement

Using LAG() on an UPDATE with CASE statements

I have a query on a Python Dashboard (using Plotly/Dash) that let users set some variables and update a table in BigQuery.

I’m having trouble with one query that has some CASE statements and using LAG().

I wrote the following query:

But I got the following error: “Analytic function not allowed in UPDATE clause…”.

I researched about it and it’s caused by the LAG function, which uses de OVER clause.

I have a column named RN on my dataset, but it could use a row_number() function if it helps.

I don’t know what to do to fix it.

Can someone help? Thanks in advance!

Advertisement

Answer

Big Query allows a FROM clause in UPDATE statements…

So, you can write a sub-query which reads from your table and uses LAG() in your CASE expression.

Then, use the UPDATE‘s WHERE clause to join that result set back on to the target table, using whatever the table’s primary key is.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement