我在名为Temp的表中有大量数据。这些数据包含重复数据。 不是整行,而是3列中的相同数据。他们是HouseNo,DateofYear,TimeOfDay。
我想只将“Temp”中的不同行复制到另一个表“ThermData”中。
基本上我想做的是将所有不同的行从Temp复制到ThermData,其中distinct(HouseNo,DateofYear,TimeOfDay)。这样的事情。
我知道我们做不到。替代我如何做到这一点。
帮助我。我已经尝试了很多东西,但还没有解决它。
示例数据。重复的值就像.... 我想根据HouseNo,DateofYear,TimeOfDay
的值删除重复的行HouseNo DateofYear TimeOfDay Count
102 10/1/2009 0:00:02 AM 2
102 10/1/2009 1:00:02 AM 2
102 10/1/2009 10:00:02 AM 2
答案 0 :(得分:0)
这是一个基于Orders表的Northwind示例。
根据(EmployeeID,ShipCity,ShipCountry)列有重复项。
如果您只执行这两行之间的代码:
/* Run everything below this line to show crux of the fix */
/* Run everything above this line to show crux of the fix */
你会看到它是如何运作的。基本上是:
(1)您在感兴趣的3列上运行GROUP BY。的(derived1Duplicates)强>
(2)然后使用这3列连接回表。 (on ords.EmployeeID = derived1Duplicates.EmployeeID and ords.ShipCity = derived1Duplicates.ShipCity and ords.ShipCountry = derived1Duplicates.ShipCountry)
(3)然后对于每个组,用基数(1,2,3,4等)标记它们(使用ROW_NUMBER())
(4)然后在每个组中保留基数为“1”的行。 (其中derived2DuplicatedEliminated.RowIDByGroupBy = 1)
Use Northwind
GO
declare @DestinationVariableTable table (
NotNeededButForFunRowIDByGroupBy int not null ,
NotNeededButForFunDuplicateCount int not null ,
[OrderID] [int] NOT NULL,
[CustomerID] [nchar](5) NULL,
[EmployeeID] [int] NULL,
[OrderDate] [datetime] NULL,
[RequiredDate] [datetime] NULL,
[ShippedDate] [datetime] NULL,
[ShipVia] [int] NULL,
[Freight] [money] NULL,
[ShipName] [nvarchar](40) NULL,
[ShipAddress] [nvarchar](60) NULL,
[ShipCity] [nvarchar](15) NULL,
[ShipRegion] [nvarchar](15) NULL,
[ShipPostalCode] [nvarchar](10) NULL,
[ShipCountry] [nvarchar](15) NULL
)
INSERT INTO @DestinationVariableTable (NotNeededButForFunRowIDByGroupBy , NotNeededButForFunDuplicateCount , OrderID,CustomerID,EmployeeID,OrderDate,RequiredDate,ShippedDate,ShipVia,Freight,ShipName,ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry )
Select RowIDByGroupBy , MyDuplicateCount , OrderID,CustomerID,EmployeeID,OrderDate,RequiredDate,ShippedDate,ShipVia,Freight,ShipName,ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry
From
(
/* Run everything below this line to show crux of the fix */
Select
RowIDByGroupBy = ROW_NUMBER() OVER(PARTITION BY ords.EmployeeID , ords.ShipCity , ords.ShipCountry ORDER BY ords.OrderID )
, derived1Duplicates.MyDuplicateCount
, ords.*
from
[dbo].[Orders] ords
join
(
select EmployeeID , ShipCity , ShipCountry , COUNT(*) as MyDuplicateCount from [dbo].[Orders] GROUP BY EmployeeID , ShipCity , ShipCountry /*HAVING COUNT(*) > 1*/
) as derived1Duplicates
on ords.EmployeeID = derived1Duplicates.EmployeeID and ords.ShipCity = derived1Duplicates.ShipCity and ords.ShipCountry = derived1Duplicates.ShipCountry
/* Run everything above this line to show crux of the fix */
)
as derived2DuplicatedEliminated
where derived2DuplicatedEliminated.RowIDByGroupBy = 1
select * from @DestinationVariableTable
强调文字 *强调文字* 强调文字