如何在将表与其自身连接时排除重复行
这是一个示例表,可帮助说明我的问题:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需从查询中删除一个字符,它就会完成工作...(将“<>”替换为“<”):
Just remove a single character from your query and it will get the job done... (replace "<>" with "<"):