令人困惑的 SELECT 语句
首先,我将向您展示与我的问题相关的示例表,然后我将提出问题。
[my_fruits]
fruit_name | fruit_id | fruit_owner | fruit_timestamp
----------------------------------------------------------------
Banana | 3 | Timmy | 3/4/11
Banana | 3 | Timmy | 4/1/11
Banana | 8 | Timmy | 5/2/11
Apple | 4 | Timmy | 2/1/11
Apple | 4 | Roger | 3/4/11
现在我想运行一个仅选择fruit_name、fruit_id 和fruit_owner 值的查询。我只想为每个水果获取一行,并且我希望通过最新时间戳来决定它。例如,此表上的完美查询将返回:
[my_fruits]
fruit_name | fruit_id | fruit_owner |
----------------------------------------------
Banana | 8 | Timmy |
Apple | 4 | Roger |
我尝试了查询:
select max(my_fruits.fruit_name) keep
(dense_rank last order by my_fruits.fruit_timestamp) fruit_name,
my_fruits.fruit_id, my_fruits.fruit_owner
from my_fruits
group by my_fruits.fruit_id, my_fruits.fruit_owner
现在的问题是返回基本上不同的水果名称、水果 ID 和水果所有者。
First I will show you example tables that my issue pertains to, then I will ask the question.
[my_fruits]
fruit_name | fruit_id | fruit_owner | fruit_timestamp
----------------------------------------------------------------
Banana | 3 | Timmy | 3/4/11
Banana | 3 | Timmy | 4/1/11
Banana | 8 | Timmy | 5/2/11
Apple | 4 | Timmy | 2/1/11
Apple | 4 | Roger | 3/4/11
Now I want to run a query that only selects fruit_name, fruit_id, and fruit_owner values. I only want to get one row per fruit, and the way I want it to be decided is by the latest timestamp. For example the perfect query on this table would return:
[my_fruits]
fruit_name | fruit_id | fruit_owner |
----------------------------------------------
Banana | 8 | Timmy |
Apple | 4 | Roger |
I tried the query:
select max(my_fruits.fruit_name) keep
(dense_rank last order by my_fruits.fruit_timestamp) fruit_name,
my_fruits.fruit_id, my_fruits.fruit_owner
from my_fruits
group by my_fruits.fruit_id, my_fruits.fruit_owner
Now the issue with that is returns basically distinct fruit names, fruit ids, and fruit owners.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于 Oracle 9i+,使用:
大多数数据库将支持在派生表/内联视图上使用自联接:
但是,如果有 2 个以上的fruit_name 记录具有相同的时间戳值,这将返回重复项。
For Oracle 9i+, use:
Most databases will support using a self join on a derived table/inline view:
However, this will return duplicates if there are 2+ fruit_name records with the same timestamp value.
如果您希望每个水果名称占一行,则必须按水果名称分组。
你想如何处理抢七是一个单独的问题。
If you want one row per fruit name, you have to group by fruit_name.
How you want to deal with tie-breaks is a separate issue.
尝试子查询:
Try a subquery:
我会通过找出您感兴趣的(fruit_name,fruit_timestamp)列表,然后将该“表”与实际的水果表分组并检索其他值来做到这一点。
这假设表中不存在具有相同值(fruit_name,fruit_timestamp)的多个条目,即元组(对)充当唯一标识符。
I would do it by finding out the list of (fruit_name, fruit_timestamp) which are of interest to you, and then grouping that "table" with the actual fruit table and retrieving the other values.
This assumes that there are not multiple entries in the table with the same value of (fruit_name, fruit_timestamp), i.e. that tuple (pair) act like a unique identifier.