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

 

posted @ 2014-02-18 14:24  Cyber9527  阅读(625)  评论(0编辑  收藏  举报