如何获得比原始表更高的行输出的SQL输出? - 事件的时间表
我有一个表如下:
event_num occurs_at_time length
1 0 10
2 10 3
3 20 10
4 30 5
预期输出:
start_time length event_type
0 10 Occurrence
10 3 Occurrence
13 7 Free Time
20 10 Occurrence
30 5 Occurrence
我很难弄清楚如何为空闲时间
在select>选择
语句中创建新行。 免费时间
每当下一个行之间的差异之间发生事件,即0。
例如,在event_num
2和event_num
3,20-10-3 = 7
之间10 + 3 = 13
将是start_time
。
我尝试使用lag()
和lead()
window> case 条款以比较下一行,但我不是确保如何在中间创建一个新行。
I have a table as follows:
event_num occurs_at_time length
1 0 10
2 10 3
3 20 10
4 30 5
Intended output:
start_time length event_type
0 10 Occurrence
10 3 Occurrence
13 7 Free Time
20 10 Occurrence
30 5 Occurrence
I'm having a hard time figuring out how to create a new row for Free Time
in a SELECT
statement. Free Time
events occur whenever the difference between the next row occurs_at_time
and previous row length
+ occurs_at_time
is > 0.
For instance, between event_num
2 and event_num
3, 20 - 10 - 3 = 7
is the length
and 10 + 3 = 13
will be the start_time
.
I tried using LAG()
and LEAD()
window functions with CASE WHEN
clauses to compare the next and previous rows, but I'm not sure how I can create a new row in the middle.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请使用-USTUPS
如果应用于您的问题中的示例数据,
Use below
if applied to sample data in your question - output is