Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bigquery Standard SQL "max_by" and "regr_slope" functions

Is there any equivalent function in Bigquery using Standard SQL for max_by and regr_slope functions. If not how to achieve this.

Thanks, Maniyar

like image 252
Maniyar Avatar asked Oct 16 '25 19:10

Maniyar


1 Answers

It does not appear that BigQuery supports either of these functions out of the box. For MAX_BY, you may just use ROW_NUMBER:

-- replacement for MAX_BY(col, val)
-- find the value for col which generates the MAX value

WITH cte AS (
    SELECT col, ROW_NUMBER() OVER (ORDER BY val DESC) rn
    FROM yourTable
)

SELECT col
FROM cte
WHERE rn = 1;

Or, you could use LIMIT if your version of BigQuery supports that:

SELECT col
FROM yourTable
ORDER BY val DESC
LIMIT 1;

As for doing linear regressions in BigQuery, I refer you to this highly upvoted Stack Overflow question and answer.

like image 143
Tim Biegeleisen Avatar answered Oct 18 '25 15:10

Tim Biegeleisen