Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all columns, but replace some with expression in Google BigQuery?

Similar to Select All Columns Except Some in Google BigQuery? - we want to SELECT * from the table, but instead of excluding some columns, we want to replace them with some expression. For example, given table with columns: name, start_date, amount, end_date, comment, we want to convert start and end from STRING to DATE. It is possible to write

SELECT 
  * EXCEPT(start_date, end_date),
  CAST(start_date AS DATE) start_date,
  CAST(end_date AS DATE) end_date

But this would change order of columns moving start and end to the end.

like image 427
Mosha Pasumansky Avatar asked Sep 02 '16 16:09

Mosha Pasumansky


People also ask

How do I SELECT all columns except one in BigQuery?

A SELECT * EXCEPT statement specifies the names of one or more columns to exclude from the result. All matching column names are omitted from the output. Note: SELECT * EXCEPT does not exclude columns that do not have names.

How do you find and replace in BigQuery?

Find and Replace: BigQuery First, make sure you click in the BigQuery development environment. Next, on Mac, hit cmd + f. On Windows, you'll need to hit control + f. We'll test this theory by trying to find and replace a variable in the monthly spending query I wrote about below.

What does except distinct do in BigQuery?

The table operator except [distinct] returns the rows of the first result except those that are also in the second.

What is coalesce in BigQuery?

COALESCE(expr[, ... ]) Description. Returns the value of the first non-null expression. The remaining expressions are not evaluated.


2 Answers

In addition to SELECT * EXCEPT, Google BigQuery also supports SELECT * REPLACE clause in Standard SQL dialect. Documentation can be found here: https://cloud.google.com/bigquery/sql-reference/query-syntax#select-list Your example will become:

SELECT * REPLACE(
  CAST(start_date AS DATE) AS start_date,
  CAST(end_date AS DATE) AS end_date)
FROM T
like image 108
Mosha Pasumansky Avatar answered Sep 21 '22 03:09

Mosha Pasumansky


I would go further and say - you can chain EXCEPT and REPLACE in same SELECT
It is not that obvious from documentation, so I thought it valuable

For example

SELECT * EXCEPT(end_date) REPLACE(
  CAST(start_date AS DATE) AS start_date)
FROM T

This will remove end_date from output end replace original start_date with casted to date start_date

like image 39
Mikhail Berlyant Avatar answered Sep 22 '22 03:09

Mikhail Berlyant