English 中文(简体)
发现/删除与另一个表格无关的重复记录
原标题:find / remove duplicate records that don t have relation to another table

我有一个称为客户信息数据库、客户目录的客户表。 我有一份命令表,其中提到客户信息表。

我需要找到并删除在订单表上持有订单的客户登记册的所有重复记录。

这就是我是如何在客户中发现我的重复,但我不知道如何通过命令表过滤,然后删除额外记录:

SELECT Name, CustomerNumber, COUNT(*) As DupeCount
FROM StagingCustomers 
WHERE ManufacturerID=15
GROUP BY Name, CustomerNumber
HAVING COUNT(CustomerNumber) > 1
ORDER BY CustomerNumber
最佳回答

添加<条码>EXISTS。 a. 在无关系的情况下检查任何记录的条款:

SELECT Name, CustomerNumber, COUNT(*) As DupeCount
FROM StagingCustomers S
WHERE ManufacturerID=15
AND NOT EXISTS (SELECT 1 from Orders WHERE CustomerID = S.CustomerID)
GROUP BY Name, CustomerNumber
HAVING COUNT(CustomerNumber) > 1
ORDER BY CustomerNumber

http://www.un.org。

删除以下记录的例子。 这将为符合上述标准的每份记录设定<代码>。 为了更明确地回答,你需要回答您的布局和关系。

UPDATE  S
SET IsDeleted = 1
FROM StagingCustomers S
WHERE ManufacturerID=15
AND NOT EXISTS (SELECT 1 from Orders WHERE CustomerID = S.CustomerID)
AND CustomerID NOT IN (SELECT CustomerNumber, MIN(CustomerID)
                       FROM StagingCustomers
                       GROUP BY CustomerNumber)
GROUP BY Name, CustomerNumber
HAVING COUNT(CustomerNumber) > 1
ORDER BY CustomerNumber
问题回答

这样做也可能是:

SELECT Name, CustomerNumber, COUNT(*) As DupeCount
FROM StagingCustomers AS SC
LEFT JOIN ORDERS AS OD ON SC.CustomerID = OD.CustomerID
WHERE OD.CustomerID IS NULL AND ManufacturerID = 15
GROUP BY Name, CustomerNumber
HAVING COUNT(CustomerNumber) > 1
ORDER BY CustomerNumber

NOTE: Did not test the Code, may need to tweak.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签