查看oracle中重复行的所有数据
我有一个包含 6 列的表格:
id
name
type_id
code
lat
long
前三个是必需的。 ID
是私钥,按序列自动插入。
我有一些重复的行,根据 name
和 type_id
相等的定义,但我想查看重复的所有数据。我可以很简单地找到这些骗子:
SELECT name
, type_id
FROM table1
GROUP BY name
, type_id
HAVING COUNT(*) > 1
但实际上查看所有信息让我感到困惑。我知道这应该很简单,但我在这里碰壁了。
I've got a table with 6 columns:
id
name
type_id
code
lat
long
The first three are required. ID
is the private key, inserted automatically with a sequence.
I have some rows that are duplicates, as defined by BOTH the name
and type_id
being equal, but i'd like to view all the data for the dupes. I can find the dupes simply enough:
SELECT name
, type_id
FROM table1
GROUP BY name
, type_id
HAVING COUNT(*) > 1
but actually viewing all the info is confounding me. I know this should be simple, but I'm hitting a wall here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您始终可以在 IN 子句中使用
GROUP BY
/HAVING
查询。这是可行的并且相对简单,但如果重复行的数量相对较大,则可能不是特别有效。使用分析函数通常会更有效,以避免再次出现问题。
根据您计划对数据执行的操作以及特定行可能有多少重复项,您可能还希望将
table1
连接到自身以获取单行中的数据You can always use the
GROUP BY
/HAVING
query in an IN clause. This works and is relatively straightforward but it may not be particularly efficient if the number of duplicate rows is relatively large.It would generally be more efficient to use analytic functions in order to avoid hitting the table a second time.
Depending on what you are planning to do with the data and how many duplicates of a particular row there might be, you also might want to join
table1
to itself to get the data in a single row会做的。
华泰
Would do it.
HTH
您可以在表上执行自连接以查找所有重复项对:
为了确保行与自身不匹配并且每对仅输出一次,我添加了
a.ROWID > b.ROWID
,适用于 Oracle。如果您使用不同的数据库,则需要采用不同的方法将它们分开。You can do a self join on the table to find all pairs of duplicates:
To make sure that a row does not match itself and each pair is only output once, I added
a.ROWID > b.ROWID
, which works for Oracle. You will need a different way to keep them apart if you use a different database.如果比较字段之一具有 NULL 值,则仍然找不到双精度值。
为了获得这些,我使用 nvl 将比较字段中的 NULL 替换为我知道该表/字段中不会出现的值。
That still does not find the doubles if one of the compared fields has a NULL value.
To get those, I use nvl to relace NULLs in the compared fields with a value that I know cannot occur in that table/field.
只需将 NULLS 设为 0...
...使用 NVL 函数即可。
Just make the NULLS 0...
...use NVL function.