'Need to generate offset value in BIG QUERY dynamically using user parameter for pagination in looker

I am trying to create a big query sql for pagination in looker. The number of rows that will be displayed at a time will be 5000 which is fixed and user inputs will be provided for page number.

Below query executes fine in BQ. This is a simple query

EXECUTE IMMEDIATE """
select distinct column1 from <table> order by column1 limit 10 offset @page
"""
USING 1 AS page;

the parameter page will be provided using user inputs.

However, when i try to execute query below, I get an error and its not executing

EXECUTE IMMEDIATE """
select distinct column1 from <table> order by column1 limit 10 offset 5000 * @page - 5000
"""
USING 1 AS page;

Here I am trying to dynamically calculate offset values based on the page that user will provide. For example,

page 1 will show first 5000 rows. page 2 will be 50002 - 5000 = 5000 page 3 will be 50003 - 5000 = 10000 so on.

However, the error I am getting as below

Invalid EXECUTE IMMEDIATE sql string <query> order by column limit 5000 offset 5000 * @page - 5000 `, Syntax error: Expected end of input but got "*" at [1:19]

Looks like I cant use multiply etc in offset values or is there anything I am missing?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source