SQLServer 添加字段分组去除重复项,用临时表

USE [DB]
 2 GO
 3 
 4 SELECT IDENTITY(INT,1,1) AS autoid
 5     , a.* 
 6 INTO [dbo].[##temp0] 
 7 FROM (SELECT DISTINCT c.CustomerId AS CustomerId
 8       ,t.TaxRule AS TaxRule
 9       FROM [dbo].[T] t
10       JOIN [dbo].[C] c
11       ON t.OldCHId = c.OldCHId
12       JOIN [dbo].[CU] u
13       ON c.CustomerId = u.CustomerId 
14       --order by c.CustomerId
15       ) AS a
16 GO    
17   
18 SELECT MAX(autoid) AS autoid INTO [dbo].[##temp1] FROM [dbo].[##temp0] t0 GROUP BY t0.CustomerId      
19 GO      
20  
21 DROP TABLE [dbo].[T]
22 
23 CREATE TABLE [dbo].[T]......
24 
25 GO
26 
27 INSERT INTO [dbo].[T] (CustomerId, TaxRule)
28 (SELECT CustomerId, TaxRule FROM [dbo].[##temp0] WHERE autoid IN (SELECT autoid FROM [dbo].[##temp1]))
29 GO
30 
31 select * from [dbo].[##temp0] order by CustomerId                                                  
32 DROP TABLE [dbo].[##temp0]
33           
34 select * from [dbo].[##temp1] order by autoid                                              
35 DROP TABLE [dbo].[##temp1]
36 
37 GO
38 
View Code

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。