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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1286366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 18:48:13  来源:igfitidea点击:

How to exclude duplicate rows when joining a table with itself

sqloraclegroup-byouter-join

提问by Rob Thomas

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:

我正在尝试将表本身连接起来,以将具有相同 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 |
+----+--------------+--------+------+--------------+--------+

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:

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

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

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

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

一般 SQL 错误。
ORA-00979: 不是 GROUP BY 表达式

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

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

回答by Roee Adler

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