如何在 SQL 中找到一个表中的所有 X,这些 X 具有来自另一个表的相同 Y 值集

发布于 2025-01-06 07:59:16 字数 282 浏览 2 评论 0原文

我有表 X,X 中的每条记录在 Y 中都有许多记录。
我想要一个简单的 SQL,它只带来 X 中的记录,而 X 中的记录与 Y 中的记录集相同。
XI 中的每组记录都希望与其他组(具有 Y 中的另一组记录)区分开来并具有某些值。

示例:

1 - 5
  - 6
2 - 3
3 - 5
  - 6
4 - 3
5 - 7

我希望它返回为:

1 - a
3 - a
2 - b
4 - b
5 - c

I have table X, and each record in X has many records in Y.
I want a simple SQL which brings just the records from X which has the same set of records from Y.
Each group of records from X I want it to be distinguished from other group (which has another set of records from Y) with some value.

Example:

1 - 5
  - 6
2 - 3
3 - 5
  - 6
4 - 3
5 - 7

I want it back as:

1 - a
3 - a
2 - b
4 - b
5 - c

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

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

发布评论

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

评论(2

三寸金莲 2025-01-13 07:59:16

也许我错过了这个问题,但 INTERSECT 运算符不会为您完成这项工作吗?

select a, b from table_x
INTERSECT
select a, b from table_y

将返回两个表中存在的所有行。 (这也可以通过简单的 JOIN 来完成,但在这种情况下 INTERSECT 更漂亮)

Maybe I'm missing the problem but won't the INTERSECT operator do this job for you?

select a, b from table_x
INTERSECT
select a, b from table_y

will return all rows that exist in both tables. (This could also be done with a simple JOIN, but in this case INTERSECT is prettier)

泪眸﹌ 2025-01-13 07:59:16

这看起来像是 wm_concat 的工作或stragg,它将为您提供独特的价值观,而无需将它们音译为某些内容。

select x.id, stragg(x.value)
  from table_x x
 group by x.id

通过您的评论,您可以保证子查询中的顺序:

select id, substr(stragg(value),1,10)
  from ( select id, value
           from table_x
          order by id, value )
 group by id

唯一的问题是您的值不会“漂亮”,它们将是 table_x 中所有可能值的串联

SQL> create table table_x ( id number, value number);

Table created.

SQL> insert into table_x values (1,5);

1 row created.

SQL> insert into table_x values (1,6);

1 row created.

SQL> insert into table_x values (2,3);

1 row created.

SQL> insert into table_x values (3,6);

1 row created.

SQL> insert into table_x values (3,5);

1 row created.

SQL> insert into table_x values (4,3);

1 row created.

SQL> insert into table_x values (5,7);

1 row created.

SQL> commit;

Commit complete.

SQL>
SQL> select id, substr(stragg(value),1,10)
  2    from ( select id, value
  3             from table_x
  4            order by id, value )
  5   group by id
  6         ;

        ID STRAGG(VALUE)
---------- ----------
         1 5;6
         2 3
         3 5;6
         4 3
         5 7

SQL>

This looks like a job for wm_concat or stragg, which will give you your unique values without you having to transliterate them to something.

select x.id, stragg(x.value)
  from table_x x
 group by x.id

And with your comment you can guarantee order in a sub-query:

select id, substr(stragg(value),1,10)
  from ( select id, value
           from table_x
          order by id, value )
 group by id

The only problem is your values won't be "pretty" they'll be a concatenation of all the possible values you have in table_x

SQL> create table table_x ( id number, value number);

Table created.

SQL> insert into table_x values (1,5);

1 row created.

SQL> insert into table_x values (1,6);

1 row created.

SQL> insert into table_x values (2,3);

1 row created.

SQL> insert into table_x values (3,6);

1 row created.

SQL> insert into table_x values (3,5);

1 row created.

SQL> insert into table_x values (4,3);

1 row created.

SQL> insert into table_x values (5,7);

1 row created.

SQL> commit;

Commit complete.

SQL>
SQL> select id, substr(stragg(value),1,10)
  2    from ( select id, value
  3             from table_x
  4            order by id, value )
  5   group by id
  6         ;

        ID STRAGG(VALUE)
---------- ----------
         1 5;6
         2 3
         3 5;6
         4 3
         5 7

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