SQL右连接,强制从右侧仅返回一个值

发布于 2024-09-14 08:16:51 字数 173 浏览 1 评论 0原文

table 1
---
id , name

table2
---
id , activity, datefield

table1 'right join' table2 将从右表 (table2) 返回 1 个以上结果。如何让它从具有最高日期的 table2 中仅返回“1”结果

table 1
---
id , name

table2
---
id , activity, datefield

table1 'right join' table2 will return more than 1 results from right table (table2) . how to make it return only "1" result from table2 with the highest date

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

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

发布评论

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

评论(5

感情洁癖 2024-09-21 08:16:52

你写的关于你的问题的信息很差,但我会尝试举一个例子来帮助你。

您有一个表“A”和一个表“B”,并且需要获取与表“A”相关的表“B”的“顶部”日期

示例表:< /em>

Table A:
 AID| NAME
----|-----
  1 |  Foo
  2 |  Bar

Table B:

BID | AID | DateField
----| ----| ----
 1  |   1 | 2000-01-01
 2  |   1 | 2000-01-02
 3  |   2 | 2000-01-01

如果执行此 sql:

SELECT * FROM A RIGHT JOIN B ON B.ID = A.ID

您将获得通过 ID 关联的 A 和 B 的所有信息(在这种理论情况下,这是两个表链接关系的公共字段)

A.AID | A.NAME | B.BID | B.AID | B.DateField
------|--------|-------|-------|--------------
  1   |   Foo  |   1   |   1   |   2000-01-01
  1   |   Foo  |   2   |   1   |   2000-01-02
  2   |   Bar  |   3   |   2   |   2000-01-01

但是您只需要表 A 中每个元素的最后一个日期(B 的顶部日期)

接下来,如果您只需要获取顶部日期,您需要按 B.AID 对查询进行分组,并仅获取顶部日期

SELECT 
      B.AID, First(A.NAME), MAX(B.DateField) 
FROM 
      A RIGHT JOIN B ON B.ID = A.ID 
GROUP BY 
      B.AID

此操作的结果是:

B.AID | A.NAME | B.DateField
------|--------|--------------
  1   |   Foo  |  2000-01-02
  2   |   Bar  |  2000-01-01

在这个结果中,我删除了一些重复的字段(例如 A.AID 和 B.AID,这是两个表之间的关系)或不需要的字段。

  • 提示:如果 SQL 中有更多表,这也适用。 sql“创建”查询,然后应用分组以使用 B 将 B 的重复次数限制为最高日期。

You write poor information about your problem, But I'll try to make an example to help you.

You have a table "A" and a table "B" and you need to fetch the "top" date of table "B" that is related with table "A"

Example tables:

Table A:
 AID| NAME
----|-----
  1 |  Foo
  2 |  Bar

Table B:

BID | AID | DateField
----| ----| ----
 1  |   1 | 2000-01-01
 2  |   1 | 2000-01-02
 3  |   2 | 2000-01-01

If you do this sql:

SELECT * FROM A RIGHT JOIN B ON B.ID = A.ID

You get all information of A and B that is related by ID (that in this theoretical case is the field that is common for both tables to link the relation)

A.AID | A.NAME | B.BID | B.AID | B.DateField
------|--------|-------|-------|--------------
  1   |   Foo  |   1   |   1   |   2000-01-01
  1   |   Foo  |   2   |   1   |   2000-01-02
  2   |   Bar  |   3   |   2   |   2000-01-01

But you require only the last date for each element of the Table A (the top date of B)

Next if you need to get only the top DATE you need to group your query by the B.AID and fetch only the top date

SELECT 
      B.AID, First(A.NAME), MAX(B.DateField) 
FROM 
      A RIGHT JOIN B ON B.ID = A.ID 
GROUP BY 
      B.AID

And The result of this operation is:

B.AID | A.NAME | B.DateField
------|--------|--------------
  1   |   Foo  |  2000-01-02
  2   |   Bar  |  2000-01-01

In this result I removed some fields that are duplicated (like A.AID and B.AID that is the relationship between the two tables) or are not required.

  • Tip: this also works if you have more tables into the sql. The sql "makes" the query and next applies a grouping for using the B to limit the repetitions of B to the top date.
浪漫之都 2024-09-21 08:16:52

在 table1.id 上右连接 table2 以从 table2 中选择 id, max = max(date)

right join table2 on on table1.id to to select id, max = max(date) from table2

心如荒岛 2024-09-21 08:16:52

分析!

测试数据:

create table t1
  (id        number       primary key,
   name      varchar2(20) not null
  );

create table t2
  (id        number not null, 
   activity  varchar2(20) not null,
   datefield date not null
  );

insert into t1 values (1, 'foo');
insert into t1 values (2, 'bar');
insert into t1 values (3, 'baz');

insert into t2 values (1, 'foo activity 1', date '2009-01-01');
insert into t2 values (2, 'bar activity 1', date '2009-01-01');
insert into t2 values (2, 'bar activity 2', date '2010-01-01');

查询:

select id, name, activity, datefield
  from (select t1.id, t1.name, t2.id as t2_id, t2.activity, t2.datefield,
               max(datefield) over (partition by t1.id) as max_datefield
          from t1
               left join t2 
                 on t1.id = t2.id
       )
 where ( (t2_id is null) or (datefield = maxdatefield) )

外部 where 子句将过滤掉 t2 元组中除最大日期之外的所有内容,但保留 t2 中没有匹配行的空行。

结果:

        ID NAME                ACTIVITY                 DATEFIELD
---------- -------- -------------------       -------------------
         1 foo           foo activity 1       2009-01-01 00:00:00
         2 bar           bar activity 2       2010-01-01 00:00:00
         3 baz

Analytics!

Test data:

create table t1
  (id        number       primary key,
   name      varchar2(20) not null
  );

create table t2
  (id        number not null, 
   activity  varchar2(20) not null,
   datefield date not null
  );

insert into t1 values (1, 'foo');
insert into t1 values (2, 'bar');
insert into t1 values (3, 'baz');

insert into t2 values (1, 'foo activity 1', date '2009-01-01');
insert into t2 values (2, 'bar activity 1', date '2009-01-01');
insert into t2 values (2, 'bar activity 2', date '2010-01-01');

Query:

select id, name, activity, datefield
  from (select t1.id, t1.name, t2.id as t2_id, t2.activity, t2.datefield,
               max(datefield) over (partition by t1.id) as max_datefield
          from t1
               left join t2 
                 on t1.id = t2.id
       )
 where ( (t2_id is null) or (datefield = maxdatefield) )

The outer where clause will filter out all but the maximum date from t2 tuples, but leave in the null row where there was no matching row in t2.

Results:

        ID NAME                ACTIVITY                 DATEFIELD
---------- -------- -------------------       -------------------
         1 foo           foo activity 1       2009-01-01 00:00:00
         2 bar           bar activity 2       2010-01-01 00:00:00
         3 baz
冷心人i 2024-09-21 08:16:52

要从查询中检索前 N 条记录,可以使用以下语法:

SELECT *
FROM (your ordered by datefield desc query with join) alias_name
WHERE rownum <= 1
ORDER BY rownum;

PS:我不熟悉 PL/SQL,所以也许我错了

To retrieve the Top N records from a query, you can use the following syntax:

SELECT *
FROM (your ordered by datefield desc query with join) alias_name
WHERE rownum <= 1
ORDER BY rownum;

PS: I am not familiar with PL/SQL so maybe I'm wrong

完美的未来在梦里 2024-09-21 08:16:52

我的解决方案是

select from table1 right join table2 on (table1.id= table2.id and table2.datefiled= (select max(datefield) from table2 where table2.id= table1.id) )

my solution is

select from table1 right join table2 on (table1.id= table2.id and table2.datefiled= (select max(datefield) from table2 where table2.id= table1.id) )

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