棘手的Oracle解析函数问题
我是 Oracle 分析函数的新手,我正在尝试找到编写查询的最佳方法。
以下是我正在使用的表的简化版本...
CREATE TABLE my_table
(
pid NUMBER NOT NULL,
my_value NUMBER,
value_date DATE NOT NULL,
CONSTRAINT pk_my_table PRIMARY KEY (pid, value_date)
);
请注意,“value_date”是主键的一部分。
对于每个“pid”,我希望查询显示 5 列:
1.pid
2.“my_value”最近7天数据的最大值
3. 7 天最大值对应的日期 (value_date)
4. my_value最近30天数据的最大值
5. 最长 30 天的相应日期 (value_date)
是否可以在没有连接的情况下执行此操作?实现这一目标的最佳方法是什么?
预先感谢您的帮助,
卡尔
I'm new to Oracle analytic functions and I'm trying to find the best way to write my query.
The following is a simplified version of a table I'm working with...
CREATE TABLE my_table
(
pid NUMBER NOT NULL,
my_value NUMBER,
value_date DATE NOT NULL,
CONSTRAINT pk_my_table PRIMARY KEY (pid, value_date)
);
Note that "value_date" is part of the primary key.
For each "pid", I'd like the query to display 5 columns:
1. pid
2. the max value of "my_value" for the most recent 7 days of data
3. the corresponding day (value_date) for the 7 day max
4. the max value of "my_value" for the most recent 30 days of data
5. the corresponding day (value_date) for the 30 day max
Is it possible to do this without a join? What is the best way to accomplish this?
Thanks in advance for any help,
Kal
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我无法在这里得到我想要的答案,所以我在 Oracle 论坛上发布了这个问题。我得到的答案正是我正在寻找的。以下查询仅通过表一次且没有任何联接即可获得答案...
如果有人想测试它,这里有一些示例数据。
感谢大家为回答这个问题所做的努力!
卡尔
I wasn't able to get the answer I was looking for here so I posted the question on the Oracle forums. The answer I got was exactly what I was looking for. The following query gets the answer with only one pass through the table and without any joins...
Incase anyone would like to test it, here is some sample data.
Thank you all for your efforts in answering this question!
Kal
我认为这对你来说是正确的解决方案。我只是在浏览器中输入它,尚未测试。我认为这应该对你有帮助......至少试图给你一个想法......
I think this is correct solution for you. I have just typed it in browser and haven't tested. I think this should help you... At least tried to give you an idea ...
您可以在同一时间间隔内一次性获取信息。即最近7天的信息、最近30天的信息。但要将这些数据放在一起,您需要一个联接:
这里我使用左外联接,因为您可能一周内没有数据,但一个月内有一些数据。
You can get information in one pass within the same time interval. I.e. information for the most recent 7 days, and information for the most recent 30 days. But to get these data together, you'll need a join:
Here I used left outer join because it's possible that you have no data within a week but have some data within a month.
此解决方案仅涉及 2 个表传递,并且不使用硬编码日期。
我使用了 2 个子查询,使用 row_number() 基本上根据 value_date 进行排序,以分别获取 7 天范围和 30 天范围内的最新日期。使用该数据集根据最大 my_value 对记录进行排名,选择排名= 1 的顶部记录并获得最大值和对应和日期。
这是输出:
This solution only involves 2 table passes and doesn't use hard coded dates.
I have used 2 subqueries using row_number() to basically sort based on value_date to get the most recent dates in 7 day range and 30 day range respectively. Using that dataset have ranked the records based on maximum my_value, picked the top record having rank=1 and got max value & corresponding and date.
Here is the output: