简体   繁体   中英

How do I convert a newly created column to a different time format in BigQuery?

I'm brand new to SQL and BigQuery. I've taken an online course on Data Analytics and part of the final project is to analyze a dataset and make a presentation.

The dataset is very large so I'd prefer to use SQL vs spreadsheets (which I'm much more familiar with, but I want to hone my SQL skills.).

  • display my new temp table with only the columns I selected
  • convert a newly created (calculated) column from INTERVAL to TIME format

I ran this query:

SELECT started_at, ended_at, rideable_type, member_casual, ended_at-started_at AS trip_length
  FROM 
  `my-project-2022-10-21.aggregate_divvy_tripdata.12mo_aggregate_table` LIMIT 1000

result from above query This does now display the table I want with only the columns I need, plus the newly created trip_length column. However that column is INTERVAL format and I want it to be TIME format. The source columns for that calculation were TIMESTAMP formats.

I know I can use CAST or EXTRACT to do this, but every time I try some version of that, I get an error.

What would a correct next step look like to convert trip_length to TIME format? Can I include that calculation in the query above or must it be a new query? I also feel like I should create a temp table to work for this project with but I'm not quite sure how to do that...

Thanks for your help!

I don't know if you really want the result in a TIME format because if the difference is more than 24h you'll get an error. You can easily get the trip duration in seconds for instance and work from there.

I also added a way to get it in a time format if it is really the goal.


timestamp_diff(ended_at, started_at, second) as duration_in_sec,

time(
  extract(hour from ended_at - started_at),
  extract(minute from ended_at - started_at),
  extract(second from ended_at - started_at)
) as time_format,

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