-1

Is the following the closest, least verbose way to emulate a VALUES clause in BigQuery?

WITH Roster AS
 (SELECT * FROM UNNEST([
    STRUCT('Adams' as LastName, 50 as SchoolID),
   ('Buchanan', 52),
   ('Coolidge', 52),
   ('Davis', 51),
   ('Eisenhower', 77)
])) SELECT * FROM Roster

The old way I was doing and what Google uses in their docs is something like:

WITH Roster AS
 (SELECT 'Adams' as LastName, 50 as SchoolID UNION ALL
  SELECT 'Buchanan', 52 UNION ALL
  SELECT 'Coolidge', 52 UNION ALL
  SELECT 'Davis', 51 UNION ALL
  SELECT 'Eisenhower', 77)
SELECT * FROM Roster

In Postgres or something I'd do:

WITH Roster(LastName, SchoolID) AS (
    VALUES('Adams', 50),
   ('Buchanan', 52),
   ('Coolidge', 52),
   ('Davis', 51),
   ('Eisenhower', 77)
) SELECT * FROM Roster

Which is quite similar to the first approach.

1

1 Answer 1

1

That's exactly right! Using UNNEST is a great approach for creating sample data. It's more efficient than using multiple UNION ALL statements, especially when dealing with larger datasets. UNNEST treats the entire array as a single operation, whereas UNION ALL would process each row individually.

Here are some helpful resources to learn more about other BigQuery techniques:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.