使用Google SQL将TIME(1-24)刻度转换为字符串(五类)

问题描述 投票:0回答:1

Schema Trimmed Results

我想将 start_time_of_day 转换为整数,以便将其分为 5 类:

清晨 03:01 - 07:00 上午 07:01 - 12:00 下午 12:01 - 17:00 晚上 17:01 - 22:00 深夜 22:01 - 03:00

Proposed operators

然后将它们重新转换回字符串以表示类别名称

我不知道该怎么做。一旦我将它们转换为数字然后将它们转换回字符串,是否需要创建一个单独的表?或者,我可以在一个查询中完成此操作吗?

string google-bigquery casting number-formatting
1个回答
0
投票

考虑以下方法(BigQuery 标准 SQL)

with categories as (
  select time(3,0,1) as start, time(7,0,0) as finish, 'early morning' as category union all
  select time(7,0,1), time(12,0,0), 'morning' union all
  select time(12,0,1), time(17,0,0), 'afternoon' union all
  select time(17,0,1), time(22,0,0), 'evening' union all
  select time(22,0,1), time(23,59,59), 'night' union all
  select time(0,0,0), time(3,0,0), 'night'
)
select t.*, category
from your_table t
left join categories
on time(timestamp_trunc(started_at, second)) between start and finish     

© www.soinside.com 2019 - 2024. All rights reserved.