PL/SQL 选择数据分区窗口

发布于 2024-08-12 01:19:32 字数 1283 浏览 3 评论 0原文

此处使用 Oracle 11g。

我试图弄清楚如何针对下面的示例表编写特定查询:

 ID TYPE   PRIORITY    STATUS   DATE
 ----------------------------------------------------
 1  Q      A           NEW      01-OCT-2009
 1  Q      A           NEW      01-OCT-2009
 1  Q      A           NEW      01-OCT-2009
 1  Q      A           NEW      01-OCT-2009
 1  Q      A           NEW      01-OCT-2009
 1  Q      A           NEW      01-OCT-2009
 1  Q      A           NEW      01-OCT-2009
 1  Q      A           NEW      01-OCT-2009
 2  R      B           NEW      01-OCT-2009
 2  R      B           NEW      01-OCT-2009
 2  R      B           NEW      01-OCT-2009
 2  R      B           NEW      01-OCT-2009
 3  R      A           NEW      01-OCT-2009
 3  Q      A           NEW      01-OCT-2009
 3  Q      A           NEW      01-OCT-2009

这是我希望查询执行的伪 PL/SQL:

 SELECT ID, TYPE
 FROM DATA_TABLE
 WHERE ROWNUM = 1 AND STATUS = 'NEW'
 GROUP BY ID, TYPE
 ORDER BY PRIORITY, DATE

我想获取下一组 ID,TYPE 具有状态 NEW 按优先级和日期排序。

在上述情况下,语句应返回 1 Q 或 3 Q 之一,但不能同时返回,因为它们具有相同的优先级和日期。如果 3 Q 设置为 STATUS='DONE',则查询应返回 1 Q。

对于第二步,我将把这些数据连接回表中,以获取我想要处理的集合的行(例如: 1 问)。这不一定是一个两步过程;如果我可以在没有连接的情况下获取要处理的行集,那就太理想了。

我希望我只是错过了一些非常简单的东西,但如果需要的话,我愿意使用分析函数进行分区。

Working with Oracle 11g here.

I'm trying to figure out how to write a specific query against a sample table below:

 ID TYPE   PRIORITY    STATUS   DATE
 ----------------------------------------------------
 1  Q      A           NEW      01-OCT-2009
 1  Q      A           NEW      01-OCT-2009
 1  Q      A           NEW      01-OCT-2009
 1  Q      A           NEW      01-OCT-2009
 1  Q      A           NEW      01-OCT-2009
 1  Q      A           NEW      01-OCT-2009
 1  Q      A           NEW      01-OCT-2009
 1  Q      A           NEW      01-OCT-2009
 2  R      B           NEW      01-OCT-2009
 2  R      B           NEW      01-OCT-2009
 2  R      B           NEW      01-OCT-2009
 2  R      B           NEW      01-OCT-2009
 3  R      A           NEW      01-OCT-2009
 3  Q      A           NEW      01-OCT-2009
 3  Q      A           NEW      01-OCT-2009

Here is the pseudo-PL/SQL of what I want the query to do:

 SELECT ID, TYPE
 FROM DATA_TABLE
 WHERE ROWNUM = 1 AND STATUS = 'NEW'
 GROUP BY ID, TYPE
 ORDER BY PRIORITY, DATE

I want to grab the next group of ID, TYPE that has a status NEW ordered by priority and date.

In the case above the statement should return either 1 Q or 3 Q, but not both, since they have the same priority and date. If 3 Q was set to STATUS='DONE' then the query should return 1 Q.

For the second step I'm going to join this data back in to the table to grab the rows for the set I want to process (eg: 1 Q). This doesn't have to be a two-step process; if I can grab the set of rows to process without the join that would be ideal.

I hope I'm just missing something really simple, but I'm open to using analytic functions for partitioning if need be.

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

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

发布评论

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

评论(1

南风起 2024-08-19 01:19:32
SELECT * FROM (SELECT ID, TYPE FROM DATA_TABLE WHERE STATUS = 'NEW' ORDER BY PRIORITY, DATE) WHERE ROWNUM = 1

那应该有效。您不想按 ID 和 TYPE 进行分组,因为您实际上并没有尝试对与给定 ID 和 TYPE 匹配的行执行任何类型的聚合。如果您只关心一列(例如日期),您可以说

SELECT ID, TYPE FROM DATA_TABLE WHERE DATE = (SELECT MIN(DATE) FROM DATA_TABLE) AND ROWNUM = 1

并避免对整个表进行排序。但我不知道如何在这里发挥作用。

SELECT * FROM (SELECT ID, TYPE FROM DATA_TABLE WHERE STATUS = 'NEW' ORDER BY PRIORITY, DATE) WHERE ROWNUM = 1

That ought to work. You don't want to group by ID and TYPE because you're not actually trying to perform any sort of aggregation on the rows matching a given ID and TYPE. If you only cared about one column (say DATE) you could say

SELECT ID, TYPE FROM DATA_TABLE WHERE DATE = (SELECT MIN(DATE) FROM DATA_TABLE) AND ROWNUM = 1

and avoid sorting the whole table. But I don't see how to make that work here.

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