Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between row in google big query

Tags:

I'm currently attempting to calculate differences between rows in google big query. I actually have a working query.

 SELECT
    id, record_time, level, lag,
    (level - lag) as diff
 FROM (
   SELECT
      id, record_time, level, 
      LAG(level) OVER (ORDER BY id, record_time) as lag
   FROM (
      SELECT
        *
      FROM
        TABLE_QUERY(MY_TABLES))
   ORDER BY
      1, 2 ASC
    )
 GROUP BY 1, 2, 3, 4
 ORDER BY 1, 2 ASC

But I'm working with big data and sometimes I have memory limit warning that does not let me execute the query. So, I would like to understand why I cant do an optimized query like bellow. I think it will allow work with more records without memory limit warning.

   SELECT
      id, record_time, level,
      level - LAG(level, 1) OVER (ORDER BY id, record_time) as diff
   FROM (
      SELECT
        *
      FROM
        TABLE_QUERY(MY_TABLES))
   ORDER BY
      1, 2 ASC

This kind of function level - LAG(level, 1) OVER (ORDER BY id, record_time) as diff, when the query is executed returns the error

Missing function in Analytic Expression

on Big Query.

I also tried to put ( ) into this function but it does not work as well.

Thanks for helping me!

like image 427
Nielsen Rechia Avatar asked Oct 11 '17 18:10

Nielsen Rechia


1 Answers

It works fine for me. Maybe you forgot to enable standard SQL? Here is an example:

WITH Input AS (
  SELECT 1 AS id, TIMESTAMP '2017-10-17 00:00:00' AS record_time, 2 AS level UNION ALL
  SELECT 2, TIMESTAMP '2017-10-16 00:00:00', 3 UNION ALL
  SELECT 1, TIMESTAMP '2017-10-16 00:00:00', 4
)
SELECT
 id, record_time, level, lag,
 (level - lag) as diff
FROM (
  SELECT
    id, record_time, level, 
    LAG(level) OVER (ORDER BY id, record_time) as lag
  FROM Input
)
GROUP BY 1, 2, 3, 4
ORDER BY 1, 2 ASC;
like image 177
Elliott Brossard Avatar answered Sep 22 '22 15:09

Elliott Brossard