按连续时间间隔对 SQL 结果进行分组(oracle sql)
您好,我的表中有以下数据:
ID-----startDate----endDate
5549 2008-05-01 4712-12-31
5567 2008-04-17 2008-04-30 1
5567 2008-05-01 2008-07-31 1
5567 2008-09-01 4712-12-31 2
5569 2008-05-01 2008-08-31
5569 2008-09-01 4712-12-31
5589 2008-04-18 2008-04-30
5589 2008-05-01 4712-12-31
5667 2008-05-01 4712-12-31
5828 2008-06-03 4712-12-31
5867 2008-06-03 4712-12-31
6167 2008-11-01 4712-12-31
6207 2008-07-01 4712-12-31
6228 2008-07-01 4712-12-31
6267 2008-07-14 4712-12-31
我正在寻找一种方法来对每个 id 的连续时间间隔进行分组以返回:
ID, 分钟(开始日期), max(endDate),
在粗体 ID 5567 5567 2008-04-17 2008-07-31 的结果中得到类似的结果
5567 2008-09-01 4712-12-31
PL/SQL 也是一个选项:)
谢谢,
Hi I have following data in the table:
ID-----startDate----endDate
5549 2008-05-01 4712-12-31
5567 2008-04-17 2008-04-30 1
5567 2008-05-01 2008-07-31 1
5567 2008-09-01 4712-12-31 2
5569 2008-05-01 2008-08-31
5569 2008-09-01 4712-12-31
5589 2008-04-18 2008-04-30
5589 2008-05-01 4712-12-31
5667 2008-05-01 4712-12-31
5828 2008-06-03 4712-12-31
5867 2008-06-03 4712-12-31
6167 2008-11-01 4712-12-31
6207 2008-07-01 4712-12-31
6228 2008-07-01 4712-12-31
6267 2008-07-14 4712-12-31
I am looking for I way to group the continuous time intervals for each id to return:
ID,
min(startDate),
max(endDate),
to have something like this in result for the bolded ID 5567
5567 2008-04-17 2008-07-31
5567 2008-09-01 4712-12-31
PL/SQL is also an option here :)
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为这将满足您的需求:
(请注意,它可能会因重叠范围而感到困惑;不知道它们是否可能出现在您的数据集中)
这是我使用的测试数据 - 基于您的测试数据,并有几个额外的条目:
I think this will do what you need:
(note that it will probably get confused by overlapping ranges; don't know if they're possible in your data set)
Here's the test data that I used - based on yours with a couple extra entries:
您可以使用如下分析函数来执行此操作:
我用您的数据得到此结果:
它是如何工作的:
You could do this with analytic functions like this:
I get this result with your data:
How it works:
您必须编写一个 PL/SQL 块示例逻辑,如下所示;
you will have to write a PL/SQL block sample logic as below;
我还没有接近要测试的实例,但是你尝试过吗?
这应该会为您提供每个 ID 的所有最早的
startDate
和最新的endDate
。 连续与否。I'm not near an instance to test, but have you tried;
This should give you all the earliest
startDate
s, and the latestendDate
s for each ID. Continuous or not.结果是:
Results is:
我一直在尝试做重叠范围但很难得到一些
有了这些数据:
I have been trying to do for overlapping ranges but getting some difficuly
with these data :