我是 SQL 的新手,我在工作中写了一些 SQL 查询。 我遇到了某种障碍,你们能帮我解决或替代吗?
这是我使用的查询(仅使用 1 行作为引用):
Select
USER_NAME,
TIME(CONVERT_TZ(START_TIME, '+00:00','America/Los_Angeles')) as 'PST Start',
TIME(CONVERT_TZ(END_TIME, '+00:00','America/Los_Angeles')) as 'PST End',
name as 'Type',
date(start_time) as 'Date',
WORKGROUP_NAME
from USER_SCHEDULE_INSTANCES
where user_name = 'prateeth'
and date(START_TIME) = '2018-01-01'
limit 1
这是输出
USER_NAME PST Start PST End Date Type WORKGROUP_NAME
prateeth 06:00:00 09:00:00 2018-01-01 Work NACS Command Center
我该怎么做才能将其分成半小时的间隔,而不使用宏并接收这样的输出:
USER_NAME PST Start PST End Date Type WORKGROUP_NAME
prateeth 06:00:00 06:30:00 2018-01-01 Work NACS Command Center
prateeth 06:30:00 07:00:00 2018-01-01 Work NACS Command Center
prateeth 07:00:00 07:30:00 2018-01-01 Work NACS Command Center
prateeth 07:30:00 08:00:00 2018-01-01 Work NACS Command Center
prateeth 08:00:00 08:30:00 2018-01-01 Work NACS Command Center
prateeth 08:30:00 09:00:00 2018-01-01 Work NACS Command Center
最佳答案
评论回答。创建一个时间表并填充您要查找的时间范围。沿着这些方向的东西:
Table Name: Dim_Time
example data:
ID starttime endtime
1 06:00 06:30
2 06:30 07:00
etc
当您需要拆分到时间范围时,在您的 from 语句中交叉连接到该表。
From USER_SCHEDULE_INSTANCES
join dim_time on 1=1
您可以在此时间表中添加第二个 ID 列,这样您也可以将此时间分割为多个“版本”。
希望已经清楚了,如果您需要更多信息,请发表评论。
关于mysql - 如何在 'Select' SQL 查询中将时间范围拆分为间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48407006/