首页 > 数据库技术 > 详细

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

时间:2014-02-18 23:05:00      阅读:877      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
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

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

原文:http://www.cnblogs.com/RocCnBlog/p/3554211.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!