计算并求和多行之间的时间差
我有一个子表,其中包含特定父记录的状态历史记录。
该表为:
Parent_id NUMBER(38)
Date_Created DATE
Status VARCHAR2(15)
示例数据:
1, sysdate-20, REQ
1, sysdate-10, INPRG
1, sysdate-5, WAIT
1, sysdate-2, INPRG
1, sysdate, COMP
对于任何特定的parent_id,如何计算parent_id 处于特定状态的总时间?鉴于计算是创建下一个状态的日期减去创建记录的日期。请记住,该状态可能会出现多次。
对于示例数据,如何计算记录处于“INPRG”状态的总时间?
它必须完全在 Oracle SQL 中完成。没有函数、过程、包等。
提前致谢。
I have a child table that contains the history of statuses for the particular parent record.
The table would be:
Parent_id NUMBER(38)
Date_Created DATE
Status VARCHAR2(15)
Sample Data:
1, sysdate-20, REQ
1, sysdate-10, INPRG
1, sysdate-5, WAIT
1, sysdate-2, INPRG
1, sysdate, COMP
For any particular parent_id, how can I calculate the total time a parent_id stayed in a particular status? Given that the calculation is the date next status was created minus the teh date record was created. Keeping in mind that the status can occur multiple times.
For the sample data, how could you calculate the total time the record was in "INPRG" status?
It has to be done entirely in Oracle SQL. No functions, procedures, packages, etc.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用分析函数
LEAD
和LAG
访问结果集中下一行或上一行的数据。这样的东西会给你每个状态的总时间You can use the analytic function
LEAD
andLAG
to access data from the next or prior row in the result set. Something like this will give you the total time in each status我主要使用 SQL Server,而不是 Oracle,所以如果我的语法有点偏差,请原谅我...
基本概念是利用
row_number
将每一行自连接到下一行并计算之间的时间他们。然后只需对这些数据进行简单的聚合即可获得您想要的答案。I primarily use SQL Server, not Oracle, so forgive me if my syntax is a little off...
The basic concept is to utilize
row_number
to self join each row to it's next row and compute the time between them. Then it's just a simple aggregation of that data to get the answer you want.