Oracle - 帮助进行查询

发布于 2024-11-24 05:17:51 字数 572 浏览 4 评论 0原文

我有以下格式的数据源:

Event Type| Date
1         | 2011-07-14 09:00
1         | 2011-07-14 09:01
1         | 2011-07-14 09:02
2         | 2011-07-14 09:30
2         | 2011-07-14 09:31
1         | 2011-07-14 10:00
1         | 2011-07-14 10:01

事件类型按发生日期排序。我需要进行一个查询,该查询将显示使用事件时的日期范围,并按日期排序。像这样:

Event Type | Date Range
1          | 2011-07-14 09:00 - 2011-07-14 09:02
2          | 2011-07-14 09:30 - 2011-07-14 09:31
1          | 2011-07-14 10:00 - 2011-07-14 10:01

你有什么提示吗?我认为这可能需要通过分析函数来完成,但我还没有找到一个像样的解决方案。

I have source of data in the following format:

Event Type| Date
1         | 2011-07-14 09:00
1         | 2011-07-14 09:01
1         | 2011-07-14 09:02
2         | 2011-07-14 09:30
2         | 2011-07-14 09:31
1         | 2011-07-14 10:00
1         | 2011-07-14 10:01

Event types are sorted by date, as they occurred. I need to make a query which will show the date ranges when events were used, sorted by date. Like this:

Event Type | Date Range
1          | 2011-07-14 09:00 - 2011-07-14 09:02
2          | 2011-07-14 09:30 - 2011-07-14 09:31
1          | 2011-07-14 10:00 - 2011-07-14 10:01

Do you have any hints? I reckon this will probably need to be done with analytic functions, but I haven't been able to come up with a decent solution yet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南汐寒笙箫 2024-12-01 05:17:51

您还可以尝试以下方法:

WITH ranked AS (
  SELECT
    EventType,
    Date,
    ROW_NUMBER() OVER (ORDER BY Date) -
      ROW_NUMBER() OVER (PARTITION BY EventType ORDER BY Date) AS GroupID
  FROM Events
)
SELECT
  EventType,
  MIN(Date) AS StartDate,
  MAX(Date) AS EndDate
FROM ranked
GROUP BY
  GroupID,
  EventType
ORDER BY
  MIN(Date)

You could also try the following approach:

WITH ranked AS (
  SELECT
    EventType,
    Date,
    ROW_NUMBER() OVER (ORDER BY Date) -
      ROW_NUMBER() OVER (PARTITION BY EventType ORDER BY Date) AS GroupID
  FROM Events
)
SELECT
  EventType,
  MIN(Date) AS StartDate,
  MAX(Date) AS EndDate
FROM ranked
GROUP BY
  GroupID,
  EventType
ORDER BY
  MIN(Date)
习惯成性 2024-12-01 05:17:51

我确信有更好的解决方案,但是这个呢?

WITH
  ordered_data AS
(
  SELECT ROW_NUMBER() OVER (ORDER BY Date) AS row_id, * FROM event_data
),
  [start_events] AS
(
  SELECT * FROM ordered_data  AS [start]
  WHERE NOT EXISTS (SELECT * FROM ordered_data WHERE row_id = [start].row_id - 1 AND event_type = [start].event_type)
),
  [end_events] AS
(
  SELECT * FROM ordered_data  AS [end]
  WHERE NOT EXISTS (SELECT * FROM ordered_data WHERE row_id = [end].row_id + 1 AND event_type = [end].event_type)
)

SELECT
  *
FROM
  [start_events]
INNER JOIN
  [end_events]
    ON [end_events].row_id = (SELECT MIN(row_id) FROM [end_events] WHERE row_id >= [start_events].row_id)

这也应该能够应对“组”中只有一个事件的情况;如 (1, 1, 2, 1, 1)

I'm sure there is a better solution, but what about this?

WITH
  ordered_data AS
(
  SELECT ROW_NUMBER() OVER (ORDER BY Date) AS row_id, * FROM event_data
),
  [start_events] AS
(
  SELECT * FROM ordered_data  AS [start]
  WHERE NOT EXISTS (SELECT * FROM ordered_data WHERE row_id = [start].row_id - 1 AND event_type = [start].event_type)
),
  [end_events] AS
(
  SELECT * FROM ordered_data  AS [end]
  WHERE NOT EXISTS (SELECT * FROM ordered_data WHERE row_id = [end].row_id + 1 AND event_type = [end].event_type)
)

SELECT
  *
FROM
  [start_events]
INNER JOIN
  [end_events]
    ON [end_events].row_id = (SELECT MIN(row_id) FROM [end_events] WHERE row_id >= [start_events].row_id)

This should also cope with scenarios where the 'group' only has one event in it; Such as (1, 1, 2, 1, 1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文