开发者

pl sql- estimate duration between timestamps

I have the table below:

ID |      ST开发者_JAVA技巧ART      |      END
A  | 11/2/2011 10:00 | 13/2/11 10:00 
A  | 15/2/2011 10:00 | 16/2/11 10:00 
A  | 18/2/2011 10:00 | 20/2/11 10:00 
B  | 11/2/2011 10:00 | 13/2/11 10:00 
C  | 14/2/2011 10:00 | 17/2/11 10:00 
D  | 19/2/2011 10:00 | 21/2/11 10:00 
D  | 25/2/2011 10:00 | 28/2/11 10:00

I want to estimate, for the repeated IDs (i.e. A, D) the duration between first END and next start, second END and third START and so go on, for the same IDs only. For example, for A, the desired result is:

START2: 15/2/2011 10:00 - END1: 13/2/11 10:00 = 2 days
START3: 18/2/2011 10:00 - END2: 16/2/11 10:00 = 2 days.

In addition, for the repeated IDs, i want the count of first events (in my example equals to 2, one for A and one for D), the count of middle repeated events ( in my example equals to 1, only for A) and the count of last. If i had an id with 10 appearances, i would have 1 first, 1 last and 8 middle events.


SELECT id,
       start,
       end, 
       start - lag(end) over (partition by id order by start)
FROM your_table

This assumes that the terms "first" and "previous" are defined by an order over the start column. If that is defined differently, you have to adjust the order by start part in my example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜