简体   繁体   中英

How to merge struct column to one data type column

I have a column of a values containing floats, integers and strings (numbers) eg 10, 10.1, "10". The problem I'm experiencing with bigquery is that I can't seem to find a way to convert all the values to the same data type eg float, without creating new columns.

`

select amount from `transactions`

Output: returns the amount column like this: |amount.integer|amount.float|amount.string|amount.provided|`

Desired output: amount

I tried to cast the column as a float and int but kept getting an error.

`

select cast(amount as float64) from `transactions`


Invalid cast from STRUCT<float FLOAT64, integer INT64, string STRING, ...> to FLOAT64 at [1:13]

`

Found a way to solve my question using the coalesce function like below.

select cast(coalesce(amount.integer, amount.float, cast (amount.string as float64))) from `transactions`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM