查看oracle中重复行的所有数据

发布于 2024-12-23 18:18:05 字数 516 浏览 3 评论 0原文

我有一个包含 6 列的表格:

  • id
  • name
  • type_id
  • code
  • lat
  • long

前三个是必需的。 ID 是私钥,按序列自动插入。

我有一些重复的行,根据 nametype_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 技术交流群。

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

发布评论

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

评论(5

初见终念 2024-12-30 18:18:05

您始终可以在 IN 子句中使用 GROUP BY/ HAVING 查询。这是可行的并且相对简单,但如果重复行的数量相对较大,则可能不是特别有效。

SELECT *
  FROM table1
 WHERE (name, type_id) IN (SELECT name, type_id
                             FROM table1
                            GROUP BY name, type_id
                           HAVING COUNT(*) > 1)

使用分析函数通常会更有效,以避免再次出现问题。

SELECT *
  FROM (SELECT id, 
               name,
               type_id,
               code,
               lat,
               long,
               count(*) over (partition by name, type_id) cnt
          FROM table1)
 WHERE cnt > 1

根据您计划对数据执行的操作以及特定行可能有多少重复项,您可能还希望将 table1 连接到自身以获取单行中的数据

SELECT a.name,
       a.type_id,
       a.id,
       b.id,
       a.code,
       b.code,
       a.lat,
       b.lat,
       a.long,
       b.long
  FROM table1 a
       JOIN table1 b ON (a.name = b.name AND
                         a.type_id = b.type_id AND
                         a.rowid > b.rowid)

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.

SELECT *
  FROM table1
 WHERE (name, type_id) IN (SELECT name, type_id
                             FROM table1
                            GROUP BY name, type_id
                           HAVING COUNT(*) > 1)

It would generally be more efficient to use analytic functions in order to avoid hitting the table a second time.

SELECT *
  FROM (SELECT id, 
               name,
               type_id,
               code,
               lat,
               long,
               count(*) over (partition by name, type_id) cnt
          FROM table1)
 WHERE cnt > 1

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

SELECT a.name,
       a.type_id,
       a.id,
       b.id,
       a.code,
       b.code,
       a.lat,
       b.lat,
       a.long,
       b.long
  FROM table1 a
       JOIN table1 b ON (a.name = b.name AND
                         a.type_id = b.type_id AND
                         a.rowid > b.rowid)
一曲琵琶半遮面シ 2024-12-30 18:18:05
SELECT * 
FROM   table1 t1 
WHERE  (t1.name,t1.type_id) in ( SELECT DISTINCT name
                                               , type_id
                                 FROM     table1
                                 GROUP BY name, type_id
                                 HAVING COUNT(*) > 1 )

会做的。

华泰

SELECT * 
FROM   table1 t1 
WHERE  (t1.name,t1.type_id) in ( SELECT DISTINCT name
                                               , type_id
                                 FROM     table1
                                 GROUP BY name, type_id
                                 HAVING COUNT(*) > 1 )

Would do it.

HTH

无人问我粥可暖 2024-12-30 18:18:05

您可以在表上执行自连接以查找所有重复项对:

SELECT 
  a.name    name
, a.type_id type_id_a
, a.code    code_a
, a.lat     lat_a
, a.long    long_a
, b.code    code_b
, b.lat     lat_b
, b.long    long_b
FROM table1 a
JOIN table1 b
ON  a.name    = b.name
AND a.type_id = b.type_id
AND a.ROWID > b.ROWID

为了确保行与自身不匹配并且每对仅输出一次,我添加了 a.ROWID > b.ROWID,适用于 Oracle。如果您使用不同的数据库,则需要采用不同的方法将它们分开。

You can do a self join on the table to find all pairs of duplicates:

SELECT 
  a.name    name
, a.type_id type_id_a
, a.code    code_a
, a.lat     lat_a
, a.long    long_a
, b.code    code_b
, b.lat     lat_b
, b.long    long_b
FROM table1 a
JOIN table1 b
ON  a.name    = b.name
AND a.type_id = b.type_id
AND a.ROWID > b.ROWID

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.

将军与妓 2024-12-30 18:18:05

如果比较字段之一具有 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.

一瞬间的火花 2024-12-30 18:18:05

只需将 NULLS 设为 0...

...使用 NVL 函数即可。

Just make the NULLS 0...

...use NVL function.

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