简体   繁体   English

如何检查2行在SQL Server的表中是否具有相同的数据?

[英]How to check if 2 rows have same data in a table in SQL Server?

I have the following query..now i have to check if the same columns have same data.. 我有以下查询..现在我必须检查相同的列是否具有相同的数据..

Can anyone help me with the query pls 谁能帮我查询请

SELECT 
   FirstName, MiddleName, LastName, Company, TaxExempt, 
   AddressTitle, AddressLine1, AddressLine2, City, [State], ZipCode, Country,
   PhoneNumber, EmailID, 
   o.OrderNumber, o.PONumber
FROM 
   CustomerAddresses ca 
    JOIN Orders o 
         ON ca.CustomerID = o.CustomerID
WHERE 
   AddressTitle='Billing'

A common trick is to group by everything and count: 一个常见的技巧是对所有内容进行分组并计数:

SELECT count(1) as [Count],
   FirstName, MiddleName, LastName, Company, TaxExempt, 
   AddressTitle, AddressLine1, AddressLine2, City, [State], ZipCode, Country,
   PhoneNumber, EmailID, 
   o.OrderNumber, o.PONumber
FROM 
   CustomerAddresses ca 
    JOIN Orders o 
         ON ca.CustomerID = o.CustomerID
WHERE 
   AddressTitle='Billing'
group by 
   FirstName, MiddleName, LastName, Company, TaxExempt, 
   AddressTitle, AddressLine1, AddressLine2, City, [State], ZipCode, Country,
   PhoneNumber, EmailID, 
   o.OrderNumber, o.PONumber

You can add: 你可以加:

having count(1) > 1

at the bottom if you only care about duplicates and don't want to see singles. 如果您只关心重复项而不想看到单身,则在底部。

Use Common Table Expressions to find all the rows where grouping them produce count larger than one. 使用“ 公共表表达式”查找所有对它们进行分组产生的行数都大于一的行。 Then join that result back to the CustomerAddress table to get the ID of each address. 然后将结果返回到CustomerAddress表以获取每个地址的ID。

WITH Billing_CTE
  (SELECT FirstName, MiddleName, LastName, Company, TaxExempt, AddressTitle, AddressLine1, AddressLine2, City, [State], ZipCode, Country, PhoneNumber, EmailID, o.OrderNumber, o.PONumber, Count(1) AS [DupCount]
   FROM CustomerAddresses ca
   JOIN Orders o ON ca.CustomerID = o.CustomerID
   WHERE AddressTitle='Billing'
   GROUP BY FirstName, MiddleName, LastName, Company, TaxExempt, AddressTitle, AddressLine1, AddressLine2, City, [State], ZipCode, Country, PhoneNumber, EmailID, o.OrderNumber, o.PONumber HAVING Count(1) > 1)
SELECT ca.CustomerId,
       o.OrderNumber,
       o.PONumber
FROM Billing_CTE b
JOIN Oderers o ON b.OrderNumber = o.OrderNumber
AND b.PONumber = o.PONumber

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

相关问题 比较SQL Server中同一表中的两行 - Comparing two rows in the same table in sql server 如果为null,则检查表中的SQL Server表和行 - Check SQL Server table and rows in table, if null create 数据表数组,检查以确定所有表是否具有相同的行数 - Array of Data tables, check to determine if all have the same number of rows 如何为SQL Server中具有相同accname帐户的行创建总和 - How can I make a total sum for rows that have the same accname account in SQL server 如何显示具有相同列值的行(ASP.NET Core / SQL Server) - How to display rows that have the same column value (ASP.NET Core / SQL Server) 如何在sql server中更新表中的选择行? - How to update selective rows in a table in sql server? 在读取同一表的同时更新sql服务器行 - update sql server rows, while reading the same table 如何检查SQL Server表中是否存在行? - How to check if a row is present in SQL Server table or not? 如何检查数据是否存在于 SQL Server 表列中,其中数据以引号重新插入 - How to check if a data is present in SQL Server table column where the data re inserted in inverted commas 如何将数据从特定表中的一列复制到SQL Server中另一表中的相同命名列 - How to copy data from one column in certain table to same named column in another table in SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM