Sql server 基于3列复制不同的记录

Sql server 基于3列复制不同的记录,sql-server,Sql Server,我在一个名为Temp的表中有大量数据。此数据由重复数据组成。 不是整行,而是3列中的相同数据。它们是HouseNo,DateofYear,TimeOfDay 我只想将“Temp”中不同的行复制到另一个表“ThermData”中 基本上,我想做的是将所有不同的行从Temp复制到不同的ThermData(HouseNo、DateofYear、TimeOfDay)。差不多吧 我知道我们不能那样做。我可以做的另一个选择 一定要帮我。我尝试过很多事情,但都没有解决 样本数据。重复的值类似于。。。。 我想

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

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

基本上,我想做的是将所有不同的行从Temp复制到不同的ThermData(HouseNo、DateofYear、TimeOfDay)。差不多吧

我知道我们不能那样做。我可以做的另一个选择

一定要帮我。我尝试过很多事情,但都没有解决


样本数据。重复的值类似于。。。。 我想根据HouseNo、DateofYear、TimeOfDay的值删除重复的行



下面是一个基于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)


强调文本*强调文本*强调文本

只复制那些没有重复的行吗?或者,有一些规则(您的问题中没有包含)用于如何将存在重复项的行减少到一行?你(向我)提的问题不太清楚。可能会添加一些样本数据和预期结果?不太清楚在这种情况下,您所说的distinct是什么意思。你们的意思是,若它出现不止一次,就不要复制,或者若它出现不止一次,就只复制其中的一条记录?恐怕还不清楚。在您的示例中,所有记录都是不同的。您可以显示一个前后表吗?临时表2中的记录示例:
(1,10/1/2009,0:00:02 AM,第4列部分数据1,第5列部分数据1)
(1,10/1/2009,0:00:02 AM,第4列部分数据2,第5列部分数据2)
。结果中需要1条记录,共5列。第4列和第5列需要哪些数据?
/* Run everything below this line to show crux of the fix */
/* Run everything above this line to show crux of the fix */
    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