简体   繁体   English

SQL选择具有(部分)重复数据的行

[英]SQL select rows that have (partially) duplicated data

I have the following database schema: 我有以下数据库架构:

Product ID | Component | ...

Product ID - a foreign key 产品ID-外键

Component - parts of the Product 组件-产品零件

For some Arcane reason a number of Records have the same Product ID & Component. 由于某些奥秘原因,许多记录具有相同的产品ID和组件。 Is there a SQL query that would return all the Product ID's & Components, that have multiple identical Components? 是否有一个SQL查询会返回所有具有多个相同组件的产品ID和组件?

Eg given the following table 例如给出下表

| Product ID | Component |
--------------------------
| 1          | c1000     |
| 1          | c1100     |
| 2          | c2000     |
| 2          | c2000     |
| 2          | c2200     |
| 3          | c3000     |

The SQL query should return: SQL查询应返回:

| Product ID | Component |
--------------------------
| 2          | c2000     |
SELECT ProductId, Component, count(*) Duplicates
 from MyTable  --  or whatever
 group by ProductId, Component
 having count(*) > 1

This will also show you how many duplicate entries there are. 这还将显示有多少重复的条目。

SELECT
  ProductId,
  Component
FROM
  Table
GROUP BY
  ProductId,
  Component
HAVING
  COUNT(*) > 1
select "Product ID", Component
from table
group by "Product ID", Component
having count(*) > 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM