0

我在一个名为 Temp 的表中有大量数据。此数据由重复项组成。不是整行,而是 3 列中的相同数据。它们是 HouseNo、DateofYear、TimeOfDay。

我只想将“Temp”中的不同行复制到另一个表“ThermData”中。

基本上我想要做的是将所有不同的行从 Temp 复制到不同的 ThermData(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
4

1 回答 1

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。 (衍生1重复)

(2) 然后您使用这 3 列连接回表。(关于 ords.EmployeeID = derived1Duplicates.EmployeeID 和 ords.ShipCity = derived1Duplicates.ShipCity 和 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

强调文字*强调文字*强调文字

于 2013-04-05T15:36:35.053 回答