简体   繁体   English

如何仅获取最近加入的配对?

[英]How to get only the recent joined pairs?

I've got two tables, where the second is connected to the first with multiple entries ( 1:n ). 我有两个表,其中第二个表通过多个条目( 1:n )连接到第一个表。

The first table has entries for an id_something (another table, but not important for now) and for every day date_day . 第一个表具有id_something (另一个表,但目前不重要)和每天date_day条目。

+-----------+
| Table ONE |
+----+------+-------+----------+----------+
| id | id_something | date_day | created  |
+----+--------------+----------+----------+
|  1 |          666 | 2019-1-1 | 2019-1-1 |
|  2 |          666 | 2019-1-1 | 2019-7-7 |
|  3 |          123 | 2019-1-1 | 2019-1-1 |
+----+--------------+----------+----------+

The second table is connected to this id and contain key-value-pairs. 第二个表与此id连接,并且包含键值对。

+-----------+
| Table TWO |
+--------+--+--+-----+
| id_one | foo | bar |
+--------+-----+-----+
|      1 |   1 |  20 |
|      1 |   2 |  21 |
|      2 |   1 |  30 |
|      2 |   2 |  31 |
|      2 |   3 |  32 |
|      3 |   1 |  10 |
+--------+-----+-----+

I want to query for all possible connections, so simple, it's a JOIN: 我想查询所有可能的连接,很简单,这是一个JOIN:

SELECT *
FROM one
LEFT JOIN two
ON two.id_one = one.id;

+----+--------------+----------+----------+--------+-----+-----+
| id | id_something | date_day | created  | id_one | foo | bar |
+----+--------------+----------+----------+--------+-----+-----+
|  1 |          666 | 2019-1-1 | 2019-1-1 |      1 |   1 |  20 |
|  1 |          666 | 2019-1-1 | 2019-1-1 |      1 |   2 |  21 |
|  2 |          666 | 2019-1-1 | 2019-7-7 |      2 |   1 |  30 |
|  2 |          666 | 2019-1-1 | 2019-7-7 |      2 |   2 |  31 |
|  2 |          666 | 2019-1-1 | 2019-7-7 |      2 |   3 |  32 |
|  3 |          123 | 2019-1-1 | 2019-1-1 |      3 |   1 |  10 |
+----+--------------+----------+----------+--------+-----+-----+

Now as you see, I also have a created field. 现在如您所见,我还有一个已created字段。 The id_something in conjunction with date_day could be the same - but I only want to have the most recent ( created DESC ) pairs with the second table. id_somethingdate_day可能是相同的-但我只想与第二张表具有最新的( created DESC )对。

So in this case, the query should return: 因此,在这种情况下,查询应返回:

+----+--------------+----------+----------+--------+-----+-----+
| id | id_something | date_day | created  | id_one | foo | bar |
+----+--------------+----------+----------+--------+-----+-----+
|  2 |          666 | 2019-1-1 | 2019-7-7 |      2 |   1 |  30 |
|  2 |          666 | 2019-1-1 | 2019-7-7 |      2 |   2 |  31 |
|  2 |          666 | 2019-1-1 | 2019-7-7 |      2 |   3 |  32 |
|  3 |          123 | 2019-1-1 | 2019-1-1 |      3 |   1 |  10 |
+----+--------------+----------+----------+--------+-----+-----+

But I can't get it to work... I tried to use DISTINCT or even a sub-query and a case construct, but it either doesn't work or is very imperformant. 但是我无法使其工作...我试图使用DISTINCT或什至子查询和case构造,但是它要么不起作用,要么非常不合适。 A group-by would also not return every joined pair, but just one single line for every id out of table one. group-by也不会返回每个加入的对,而对于表一中的每个id只返回一行。

What am I missing to achieve my wished result? 我错过了什么才能达到理想的结果?

(if no Oracle-specific synthax would be used, that would be a bonus.) (如果不使用特定于Oracle的synthax,那将是一个好处。)

You can use analytic functions: 您可以使用分析功能:

SELECT *
FROM (SELECT o.* ROW_NUMBER() OVER (PARTITION BY id ORDER BY created DESC) as seqnum
      FROM one o
     ) o LEFT JOIN
     two t
     ON t.id_one = o.id
WHERE o.seqnum = 1;

use row_number() take the most recent id_something and then use join 使用row_number()获取最新的id_something,然后使用join

select a.*,b.* from     
(
select *,
row_number()over(partition by id_something order by created desc) rn  from one
) a join two b ON b.id_one = a.id;
 where rn=1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM