Oracle - 在记录对之间进行分组
我有一个日志表,其中包含如下所示的数据:
ID MSG DATE --------------------------------------------- 1 TEst 2010-01-01 09:00:00 2 Job Start 2010-01-01 09:03:00 3 Do something 2010-01-01 09:03:10 4 Do something else 2010-01-01 09:03:12 5 Do something 2010-01-01 09:04:19 6 Job End 2010-01-01 09:06:30 7 Job Start 2010-01-01 09:18:03 8 Do something 2010-01-01 09:18:17 9 Do other thing 2010-01-01 09:19:48 10 Job End 2010-01-01 09:20:27
它包含(除其他外)由应用程序编写的消息。我有兴趣为所有此类“作业开始”和“作业结束”对生成“作业开始”记录和“作业结束”记录之间写入的所有内容的报告。理想情况下,报告应如下所示:(
BATCH_NUM ID MSG DATE --------------------------------------------------------- 1 3 Do something 2010-01-01 09:03:10 1 4 Do something else 2010-01-01 09:03:12 1 5 Do something 2010-01-01 09:04:19 2 8 Do something 2010-01-01 09:18:17 2 9 Do other thing 2010-01-01 09:19:48
添加批次之间的换行符以使其更易于阅读)
输出报告省略“作业开始”和“作业结束”消息,以及“TEst”消息(存在于一对之外) 。
我什至不知道从哪里开始编写这种查询,或者使用 PL/SQL 是否更好,哎呀,我什至不确定是否有一个 我正在尝试做的事情的正确技术术语;)
(oracle 版本是 10g)。
I have a logging table that contains data that looks like this:
ID MSG DATE --------------------------------------------- 1 TEst 2010-01-01 09:00:00 2 Job Start 2010-01-01 09:03:00 3 Do something 2010-01-01 09:03:10 4 Do something else 2010-01-01 09:03:12 5 Do something 2010-01-01 09:04:19 6 Job End 2010-01-01 09:06:30 7 Job Start 2010-01-01 09:18:03 8 Do something 2010-01-01 09:18:17 9 Do other thing 2010-01-01 09:19:48 10 Job End 2010-01-01 09:20:27
It contains (among other things) messags written by the application. I am interested in producing a report of everything that gets written between a "Job Start" record and a "Job End" record, for all such pairs of "Job Start" and "Job End". Ideally the report would look like this:
BATCH_NUM ID MSG DATE --------------------------------------------------------- 1 3 Do something 2010-01-01 09:03:10 1 4 Do something else 2010-01-01 09:03:12 1 5 Do something 2010-01-01 09:04:19 2 8 Do something 2010-01-01 09:18:17 2 9 Do other thing 2010-01-01 09:19:48
(line break between batches added to make it easier to read)
The output report omits the "Job STart and "Job End" messages, as well as the "TEst" message (which exists outside a pair of "Job Start" and "Job End".
I'm not even sure where to begin writing this kind of query, or if it's better to go with PL/SQL for this. Heck, I'm not even sure if there's a proper technical term for what I'm trying to do. ;)
(oracle version is 10g)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我确信有一种更具分析性的方法可以做到这一点,但我用标量子查询来构建窗口。
I'm sure there's a more analytic-ish way to do this, but I'm cheating with scalar subqueries to build the windows.
以下示例使用与 Adam Musch 的答案大致相同的想法,但在连接到日志记录语句的单个选择中获取日志记录集的开始和结束,而不是使用标量子查询。
我认为 DENSE_RANK() 不能再直接使用,因为没有可用于分区的列。
此外,这些解决方案中的任何一个都假设日志集永远不会重叠。如果第二组在第一组结束之前开始,那就是一个全新的问题......
The following sample uses much the same idea as Adam Musch's answer, but gets the logging sets' start and end in a single select which is joined to the logging statement, rather than using scalar sub-queries.
I don't think DENSE_RANK() can be used any more directly because there is no column available to partition on.
Also, either of these solutions assume that the will never be overlapping sets of logs. If a second set starts before the first one ends, it's a whole new problem...
为了以防万一,这里有一种不使用窗口函数来完成相同技巧的方法。
Here's a way of doing the same trick without using window functions, just in case.