我有两列工作日,像这样在这些工作日之间的天数:
Monday | Saturday | 13
Tuesday | Friday | 54
Friday | Wednesday | 10
等我需要使用SQL来创建另一个包含包含两个工作日之间所有13或54个工作日的字符串的列。类似于“星期一,星期二,星期三,...星期一,星期二...星期五,星期六”
这如何完成? 谢谢。
答案 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