获取一周中接下来n天的列表?

时间:2018-06-30 00:08:20

标签: sql string parsing google-bigquery

我有两列工作日,像这样在这些工作日之间的天数:

Monday  | Saturday    | 13
Tuesday | Friday      | 54
Friday  | Wednesday   | 10

等我需要使用SQL来创建另一个包含包含两个工作日之间所有13或54个工作日的字符串的列。类似于“星期一,星期二,星期三,...星期一,星期二...星期五,星期六”

这如何完成? 谢谢。

1 个答案:

答案 0 :(得分:2)

以下是用于BigQuery标准SQL

#standardSQL
SELECT start, duration,
  REGEXP_EXTRACT(
    REPEAT('Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday,',CAST(duration/7+1 AS INT64)),
    CONCAT(r'', start, r'(?:(?:,\w+){', CAST(duration - 1 AS STRING), '})')
  ) days_list
FROM `project.dataset.table`   

您可以使用以下问题中的虚拟数据来测试/玩游戏

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 'Monday' start, 'Saturday' finish, 13 duration UNION ALL
  SELECT 'Tuesday', 'Friday', 54 UNION ALL
  SELECT 'Friday', 'Wednesday', 10 
)
SELECT start, duration,
  REGEXP_EXTRACT(
    REPEAT('Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday,', CAST(duration / 7 + 1 AS INT64) ), 
    CONCAT(r'', start, r'(?:(?:,\w+){', CAST(duration - 1 AS STRING), '})')
  ) days_list
FROM `project.dataset.table`   

结果为

Row start   duration    days_list    
1   Monday  13  Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday   
2   Tuesday 54  Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday    
3   Friday  10  Friday,Saturday,Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday