从不规则日期中查找每小时的百分比
我有一个如下表,每个条目在给定时间的状态发生变化。 状态可以重复,因为其他列有子状态信息。
如何获取每种状态的时间百分比(例如,小时)?
NAME STATUS_CHANGE_TIME STATUS
foo 15-MAY-11 18:52 A
foo 15-MAY-11 18:38 A
foo 15-MAY-11 18:33 B
foo 15-MAY-11 16:53 A
foo 15-MAY-11 16:47 B
foo 15-MAY-11 13:37 A
foo 15-MAY-11 13:33 C
foo 15-MAY-11 10:23 C
foo 15-MAY-11 10:17 A
foo ...
期望回报:
HH24 STATUS PERCENT
10 ...
11 C 100 (No entries; last change was to C)
12 C 100 "" ""
13 C 62
13 A 38 (From C to A at :37 with 23 mins left; 23/60 ~ 38%)
14 A 100
15 A 100
16 A 90 (= A for first 47 minutes, then for another 7)
16 B 10 (16:53 - 16:47 = 6 minutes or 10% of an hour)
17 A 100
18 ... etc.
I have a table like the following, each entry a change in STATUS on the given time.
The status can be repeated because other columns have sub-status information.
How can I get a percentage time for each status by, say, hour?
NAME STATUS_CHANGE_TIME STATUS
foo 15-MAY-11 18:52 A
foo 15-MAY-11 18:38 A
foo 15-MAY-11 18:33 B
foo 15-MAY-11 16:53 A
foo 15-MAY-11 16:47 B
foo 15-MAY-11 13:37 A
foo 15-MAY-11 13:33 C
foo 15-MAY-11 10:23 C
foo 15-MAY-11 10:17 A
foo ...
Desired return:
HH24 STATUS PERCENT
10 ...
11 C 100 (No entries; last change was to C)
12 C 100 "" ""
13 C 62
13 A 38 (From C to A at :37 with 23 mins left; 23/60 ~ 38%)
14 A 100
15 A 100
16 A 90 (= A for first 47 minutes, then for another 7)
16 B 10 (16:53 - 16:47 = 6 minutes or 10% of an hour)
17 A 100
18 ... etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好问题,这是一个有趣的挑战!
您需要一个辅助表来存储每个时间段(在本例中为小时),然后在状态更新重叠的地方连接到它。
LEAD()
可以获取下一个状态条目来检查它是什么时候,而GREATEST()
和LEAST()
可以计算出当前时间适用于每小时状态的开始/结束。当然,通过示例更容易解释这一点。这是所需的
HOURS
表:以下只是您问题中的测试数据的总体情况。
现在这是获取所需百分比的 select 语句。
Great question, this was an interesting challenge!
What you need is an ancillary table to store each time division (in this case, hours), then join to it where the status updates overlap.
LEAD()
can grab the next status entry to check when it was, andGREATEST()
andLEAST()
can figure out which time is applicable for the start/end of the status for each hour.Of course, this is much easier explained in an example. Here is the
HOURS
table needed:The following is just population of your test data from your question.
Now here is the select statement to get the required percentages.