Search code examples
sqlgoogle-bigquerysyntax-errortemp-tables

Big Query SQL syntax error using the WITH clause


I am learning SQL using Big Query and I am facing an error I'm not being able to fixe, so I would really appreacitte your help. I am using the WITH clause to create a temporary table and a syntax error appears consistently near the last parenthesis.

WITH longest_used_bike AS(
  SELECT 
    bikeid,
    SUM(duration_minutes) AS trip_duration
  FROM 
    `bigquery-public-data.austin_bikeshare.bikeshare_trips` 
  GROUP BY
    bikeid
  ORDER BY
    trip_duration DESC
  LIMIT 1000
  )

Does anyone understand where the error is? Thank you so much in advance!

I already tried adding one more set of paranthesis and changing their place, as well as deleting "AS".


Solution

  • I think one of the column names (bikeid) is incorrect, and your statement is incomplete (no SELECT), also the ORDER BY and LIMIT clauses should be on the results. This works for me:

    WITH longest_used_bike AS
    (
      SELECT 
        bike_id,
        SUM(duration_minutes) AS trip_duration
      FROM 
        `bigquery-public-data.austin_bikeshare.bikeshare_trips` 
      GROUP BY
        bike_id
    )
    SELECT *
    FROM longest_used_bike
    ORDER BY trip_duration DESC
    LIMIT 1000
    ;