Sql 一天,周二24小时,周三3小时?这背后的逻辑是,我需要打破每天的总小时数。例如:如果开始日期=2016年1月1日15:30,结束日期=2016年1月2日00:30,则我需要在开始日期之前与集团进行拆分:2016年1月1日8+0.5=8.5。这将持续到本周末
Sql 一天,周二24小时,周三3小时?这背后的逻辑是,我需要打破每天的总小时数。例如:如果开始日期=2016年1月1日15:30,结束日期=2016年1月2日00:30,则我需要在开始日期之前与集团进行拆分:2016年1月1日8+0.5=8.5。这将持续到本周末,sql,amazon-redshift,Sql,Amazon Redshift,一天,周二24小时,周三3小时?这背后的逻辑是,我需要打破每天的总小时数。例如:如果开始日期=2016年1月1日15:30,结束日期=2016年1月2日00:30,则我需要在开始日期之前与集团进行拆分:2016年1月1日8+0.5=8.5。这将持续到本周末。但在一周的最后一天(星期五),总数将反映为8.5,这是正确的计算。然而,我需要的是将周六出版的额外的0.5小时(周六出版)分开(即按截止日期分组)完美,实际上我喜欢在这里使用union。我使用下面的例子,case when trunc(sta
一天,周二24小时,周三3小时?这背后的逻辑是,我需要打破每天的总小时数。例如:如果开始日期=2016年1月1日15:30,结束日期=2016年1月2日00:30,则我需要在开始日期之前与集团进行拆分:2016年1月1日8+0.5=8.5。这将持续到本周末。但在一周的最后一天(星期五),总数将反映为8.5,这是正确的计算。然而,我需要的是将周六出版的额外的0.5小时(周六出版)分开(即按截止日期分组)完美,实际上我喜欢在这里使用union。我使用下面的例子,case when trunc(start_time)trunc(end_time)然后cast(to_char(start_time,'yyyyy-mm-dd 23:59:59')作为时间戳)或者cast(to_char(end_time,'yyyyy-mm-dd hh24:mi:ss')作为时间戳)结束作为st_t1union case当trunc(start_time)trunc(end_time)时然后强制转换(到字符(结束时间,'yyyy-mm-dd 00:00:00')作为时间戳)或者强制转换(到字符(结束时间,'yyyy-mm-dd hh24:mi:ss')作为时间戳)结束为st_t2
user start_date end_date difference
Alex 7/25/2016 16:00 7/26/2016 0:30 8.5
Alex 7/24/2016 16:00 7/25/2016 0:30 8.5
Alex 7/21/2016 16:00 7/22/2016 0:30 8.5
Alex 7/20/2016 16:00 7/21/2016 0:30 8.5
Alex 7/19/2016 16:00 7/20/2016 0:30 8.5
Alex 7/18/2016 16:00 7/19/2016 0:30 8.5
Alex 7/17/2016 16:00 7/18/2016 0:30 8.5
Alex 7/14/2016 16:00 7/15/2016 0:30 8.5
Alex 7/13/2016 16:00 7/14/2016 0:30 8.5
Alex 7/12/2016 16:00 7/13/2016 0:30 8.5
Alex 7/11/2016 16:00 7/12/2016 0:30 8.5
Alex 7/10/2016 16:00 7/11/2016 0:30 8.5
User Date difference
Alex 7/25/2016 8.5
Alex 7/24/2016 8.5
Alex 7/22/2016 0.5
Alex 7/21/2016 8.0
Alex 7/20/2016 8.5
Alex 7/19/2016 8.5
Alex 7/18/2016 8.5
Alex 7/17/2016 8.5
Alex 7/15/2016 0.5
Alex 7/14/2016 8.0
Alex 7/13/2016 8.5
Alex 7/12/2016 8.5
Alex 7/11/2016 8.5
Alex 7/10/2016 8.5
round(cast(datediff(seconds, start_date, end_date) as decimal)/3600,2)
create temporary table _test (user varchar(20), start_date timestamp, end_date timestamp);
insert into _test values ('Alex', '7/25/2016 16:00', '7/26/2016 0:30'), ('Alex', '7/24/2016 16:00', '7/25/2016 0:30'), ('Alex', '7/21/2016 16:00', '7/22/2016 0:30'), ('Alex', '7/20/2016 16:00', '7/21/2016 0:30'), ('Alex', '7/19/2016 16:00', '7/20/2016 0:30'), ('Alex', '7/18/2016 16:00', '7/19/2016 0:30'), ('Alex', '7/17/2016 16:00', '7/18/2016 0:30'), ('Alex', '7/14/2016 16:00', '7/15/2016 0:30'), ('Alex', '7/13/2016 16:00', '7/14/2016 0:30'), ('Alex', '7/12/2016 16:00', '7/13/2016 0:30'), ('Alex', '7/11/2016 16:00', '7/12/2016 0:30'), ('Alex', '7/10/2016 16:00', '7/11/2016 0:30');
select user, trunc(start_time) as date1,
SUM(case when id = 1 then round(cast(datediff(seconds, start_time, st_t1) as decimal)/3600,2) end) as SCHEDULE
from
(
select user, start_time,
case when trunc(start_time) <> trunc(end_time) then cast(to_char(start_time,'yyyy-mm-dd 23:59:59') as timestamp) else cast(to_char(end_time,'yyyy-mm-dd hh24:mi:ss') as timestamp) end as st_t1
from table1 a
where id = 1
group by user_name, trunc(start_time)
union
select user_name, trunc(end_time) as date1,
SUM(case when id = 1 then round(cast(datediff(seconds, st_t2, end_time) as decimal)/3600,2) end) as SCHEDULE
from
(
select user_name, end_time,
case when trunc(start_time) <> trunc(end_time) then cast(to_char(end_time,'yyyy-mm-dd 00:00:00') as timestamp) else cast(to_char(end_time,'yyyy-mm-dd hh24:mi:ss') as timestamp) end as st_t2
from table1 a
where id = 1
)
group by user, trunc(end_time)