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

Time:10-08

I have a table in bigquery like below:

enter image description here

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?

CodePudding user response:

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

enter image description here

output is

enter image description here

  • Related