简体   繁体   中英

Add time to date based on the name of a column in bigquery

I have a table in bigquery like below:

在此处输入图像描述

I want to create a table and add hour to start_time based on the index of value, for example start_time for value_1 will be 2021-02-01 1:00:00 UTC for value_2 will be 2021-02-01 2:00:00 UTC The final table only has 3 columns project as string, start_time as datetime and value as numeric. Any hints?

Consider below approach

select project, 
  timestamp_add(start_time, interval cast(replace(col, 'value_', '') as int64) hour) as start_time, 
  value
from your_table
unpivot (value for col in (value_0,  value_1,  value_2,  value_3))    

if applied to dummy data as in your question

在此处输入图像描述

output is

在此处输入图像描述

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