SQL查找多行重复项

发布于 2024-09-24 21:04:11 字数 636 浏览 2 评论 0原文

在我的工作中,我们将数据存储在数据库中,但数据未标准化。我正在寻找一种方法来查找重复的数据。

我们的数据库有 3 行列,名称、状态、策略

该数据可能看起来像这样:

OldTable:
Name | State | Strat
-----+-------+------
A    | M     | 1
A    | X     | 3
B    | T     | 6
C    | M     | 1
C    | X     | 3
D    | X     | 3

我想做的是将数据移动到两个表,一个包含名称,另一个包含状态和策略集,这样它看起来更像这样

NewTable0:
Name | StratID
-----+--------
A    | 1
B    | 2
C    | 1
D    | 3

NewTable1:
StratID | State | Strat
--------+-------+------
1       | M     | 1    
1       | X     | 3
2       | T     | 6
3       | X     | 3

所以在数据示例中 A 和 C 会重复,但 D 不会。我将如何查找和/或识别这些重复项?

At my worki we have data stored in a database, the data is not normalized. I am looking for a way to find what data was duplicated.

Our Data base has 3 rows columns, Name, State, Strategy

This data might looks something like this:

OldTable:
Name | State | Strat
-----+-------+------
A    | M     | 1
A    | X     | 3
B    | T     | 6
C    | M     | 1
C    | X     | 3
D    | X     | 3

What I'd like to do is move the data to two tables, one containing the name the other containing the set of State and Strats so it would look more like this

NewTable0:
Name | StratID
-----+--------
A    | 1
B    | 2
C    | 1
D    | 3

NewTable1:
StratID | State | Strat
--------+-------+------
1       | M     | 1    
1       | X     | 3
2       | T     | 6
3       | X     | 3

So in the data example A and C would be duplicates, but D would not be. How would I go about finding and/or identifying these duplicates?

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

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

发布评论

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

评论(2

猫卆 2024-10-01 21:04:11

尝试:

SELECT OT1.Name Name1, OT2.Name Name2
FROM OldTable OT1
JOIN OldTable OT2 ON OT1.Name < OT2.Name AND 
                     OT1.State = OT2.State AND 
                     OT1.Strat = OT2.Strat
GROUP BY OT1.Name, OT2.Name 
HAVING COUNT(*) = (SELECT COUNT(*) FROM OldTable TC1 WHERE TC1.NAME = OT1.NAME) 
   AND COUNT(*) = (SELECT COUNT(*) FROM OldTable TC2 WHERE TC2.NAME = OT2.NAME)

Try:

SELECT OT1.Name Name1, OT2.Name Name2
FROM OldTable OT1
JOIN OldTable OT2 ON OT1.Name < OT2.Name AND 
                     OT1.State = OT2.State AND 
                     OT1.Strat = OT2.Strat
GROUP BY OT1.Name, OT2.Name 
HAVING COUNT(*) = (SELECT COUNT(*) FROM OldTable TC1 WHERE TC1.NAME = OT1.NAME) 
   AND COUNT(*) = (SELECT COUNT(*) FROM OldTable TC2 WHERE TC2.NAME = OT2.NAME)
暗喜 2024-10-01 21:04:11

您可以通过将名称分组在一起并仅列出有多个记录的名称来找到这一点:

SELECT OldTable.Name, COUNT(1) Duplicates
FROM OldTable
GROUP BY OldTable.Name
HAVING Duplicates > 1

应输出:

OldTable:
Name | Duplicates
-----+------------
A    | 2
C    | 2

You could find this out by grouping the Names together, and only listing those where there is more than one record:

SELECT OldTable.Name, COUNT(1) Duplicates
FROM OldTable
GROUP BY OldTable.Name
HAVING Duplicates > 1

Should output:

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