今天把题目6:关于固定时间段内的工作时间求和的答案写一下,看一下和大家想的一样不~
with t_x as ---用户参数输入(select &输入开始时间 as start_time ,&输入结束时间 as end_time from dual),t10 as ---用于对重复数据去重和对最后无out数据的容错(select distinct id,gid,activity,txntimestampfrom system.aareport union allselect distinct id,gid,'out' as activity,date'4712-12-31' as txntimestampfrom system.aareport),t2 as --- 对数据进行编号(select id,gid,activity,txntimestamp,row_number()over(partition by id,gid order by txntimestamp) as flagfrom t10)-----将开始时间和结束时间写在同一行中,并对实际时间和传入时间进行对比选取-----最后计算总时间的和select t3.id,t3.gid,min(greatest(t3.txntimestamp,t5.start_time)) as start_time ,max(least(t4.txntimestamp,t5.end_time)) as end_time ,sum(least(t4.txntimestamp,t5.end_time)-greatest(t3.txntimestamp,t5.start_time))*24 as sum_timefrom t2 t3 , t2 t4,t_x t5where t3.id=t4.id and t3.gid =t4.gid andt3.flag=t4.flag-1 and t3.txntimestamp <=t5.end_timeand t4.txntimestamp >=t5.start_timeand t3.activity ='in'and t4.activity ='out'group by t3.id ,t3.gid
刚开始编写文章,很多地方都有不足,希望以后可以为大家贡献出更好的文章,谢谢大家!有哪里不明白的地方可以和作者联系〜
没有关注的也可以关注下公众号〜再次感谢



最后修改时间:2021-04-19 17:16:55
文章转载自SQL大数据开发,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




