如何在将表与其自身连接时排除重复行

发布于 2024-08-01 13:36:02 字数 2198 浏览 6 评论 0原文

这是一个示例表,可帮助说明我的问题:

mysql> select * from test;
+----+--------------+--------+
| id | type         | siteid |
+----+--------------+--------+
|  1 | First Visit  |    100 |
|  2 | Second Visit |    100 |
|  3 | First Visit  |    300 |
|  4 | First Visit  |    400 |
|  5 | Second Visit |    500 |
|  6 | Second Visit |    600 |
+----+--------------+--------+

我试图将表自身连接起来,将具有相同 siteid 值的行聚集在一起。 这是我的尝试:

mysql> select * from test T1
    -> LEFT OUTER JOIN test T2 on T1.siteid = T2.siteid and T1.id <> T2.id;
+----+--------------+--------+------+--------------+--------+
| id | type         | siteid | id   | type         | siteid |
+----+--------------+--------+------+--------------+--------+
|  1 | First Visit  |    100 |    2 | Second Visit |    100 |
|  2 | Second Visit |    100 |    1 | First Visit  |    100 |
|  3 | First Visit  |    300 | NULL | NULL         |   NULL |
|  4 | First Visit  |    400 | NULL | NULL         |   NULL |
|  5 | Second Visit |    500 | NULL | NULL         |   NULL |
|  6 | Second Visit |    600 | NULL | NULL         |   NULL |
+----+--------------+--------+------+--------------+--------+

这基本上是我正在寻找的结果,除了第一两行。 我想消除其中之一。 所以,我尝试了以下操作:

mysql> select * from test T1
    -> LEFT OUTER JOIN test T2 on T1.siteid = T2.siteid and T1.id <> T2.id
    -> GROUP BY T1.siteid;
+----+--------------+--------+------+--------------+--------+
| id | type         | siteid | id   | type         | siteid |
+----+--------------+--------+------+--------------+--------+
|  1 | First Visit  |    100 |    2 | Second Visit |    100 |
|  3 | First Visit  |    300 | NULL | NULL         |   NULL |
|  4 | First Visit  |    400 | NULL | NULL         |   NULL |
|  5 | Second Visit |    500 | NULL | NULL         |   NULL |
|  6 | Second Visit |    600 | NULL | NULL         |   NULL |
+----+--------------+--------+------+--------------+--------+

这正是我正在寻找的输出。 然而,我逐渐了解到这不是使用 GROUP BY 的标准方法,并且上述语句在 ORACLE 上失败,给我一个

常规 SQL 错误。
ORA-00979:不是 GROUP BY表达式

任何人都可以提供一些关于如何获得像最后一个表这样的结果并且适用于 ORACLE 的帮助吗?

Here's a sample table to help illustrate my problem:

mysql> select * from test;
+----+--------------+--------+
| id | type         | siteid |
+----+--------------+--------+
|  1 | First Visit  |    100 |
|  2 | Second Visit |    100 |
|  3 | First Visit  |    300 |
|  4 | First Visit  |    400 |
|  5 | Second Visit |    500 |
|  6 | Second Visit |    600 |
+----+--------------+--------+

I'm trying to join the table upon itself, to pull together rows that have the same siteid value. Here's my attempt:

mysql> select * from test T1
    -> LEFT OUTER JOIN test T2 on T1.siteid = T2.siteid and T1.id <> T2.id;
+----+--------------+--------+------+--------------+--------+
| id | type         | siteid | id   | type         | siteid |
+----+--------------+--------+------+--------------+--------+
|  1 | First Visit  |    100 |    2 | Second Visit |    100 |
|  2 | Second Visit |    100 |    1 | First Visit  |    100 |
|  3 | First Visit  |    300 | NULL | NULL         |   NULL |
|  4 | First Visit  |    400 | NULL | NULL         |   NULL |
|  5 | Second Visit |    500 | NULL | NULL         |   NULL |
|  6 | Second Visit |    600 | NULL | NULL         |   NULL |
+----+--------------+--------+------+--------------+--------+

This is basically the result I'm looking for, except for the 1st 2 rows. I'd like to eliminate one of those. So, I tried the following:

mysql> select * from test T1
    -> LEFT OUTER JOIN test T2 on T1.siteid = T2.siteid and T1.id <> T2.id
    -> GROUP BY T1.siteid;
+----+--------------+--------+------+--------------+--------+
| id | type         | siteid | id   | type         | siteid |
+----+--------------+--------+------+--------------+--------+
|  1 | First Visit  |    100 |    2 | Second Visit |    100 |
|  3 | First Visit  |    300 | NULL | NULL         |   NULL |
|  4 | First Visit  |    400 | NULL | NULL         |   NULL |
|  5 | Second Visit |    500 | NULL | NULL         |   NULL |
|  6 | Second Visit |    600 | NULL | NULL         |   NULL |
+----+--------------+--------+------+--------------+--------+

This is exactly the output I'm looking for. However, I come to learn this isn't the standard way to use GROUP BY, and the above statement fails on ORACLE, giving me a

General SQL error.
ORA-00979: not a GROUP BY expression

Can anyone offer some help on how to get the results like the last table and that works with ORACLE?

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

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

发布评论

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

评论(1

ヤ经典坏疍 2024-08-08 13:36:02

只需从查询中删除一个字符,它就会完成工作...(将“<>”替换为“<”):

select * from test T1
LEFT OUTER JOIN test T2 on T1.siteid = T2.siteid and T1.id < T2.id

Just remove a single character from your query and it will get the job done... (replace "<>" with "<"):

select * from test T1
LEFT OUTER JOIN test T2 on T1.siteid = T2.siteid and T1.id < T2.id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文