Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
524 views
in Technique[技术] by (71.8m points)

sql - Oracle, split a time duration row by one hour period

I'm working on oracle db and I'm not quite good at Oracle. I'm trying to split a row by one hour period.

For example, if a time row has given as below,

Start_time(yyyy/mm/dd hh24:mi:ss) | End_time(yyyy/mm/dd hh24:mi:ss)

2013/09/01 09:30:00         2013/09/01 11:38:59

The result I want to see like this:

2013/09/01 09:30:00         2013/09/01 09:59:59  
2013/09/01 10:00:00         2013/09/01 10:59:59
2013/09/01 11:00:00         2013/09/01 11:38:59

I've been searched how to do it but I couldn't find one.
But I guess It can be done by using 'CONNECT BY'.
Any help will be great. Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have bulit a basic query, you can work around it and get what you want.

select greatest(Start_time, trunc(Start_time+(level-1)/24, 'hh24')), 
least(End_time, trunc(Start_time+(level)/24, 'hh24'))
from log_table 
connect by level <= floor((dt1-dt2)*24)+1;

Example at sqlfiddle:

http://sqlfiddle.com/#!4/82625/29


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...