• SQL Row_Number() 删除重复数据(单字段OR多字段) 长沙


    --------------------------删除单字段重复-----------------------------------------------
    
    If Exists(Select * From tempdb.Information_Schema.Tables Where Table_Name Like '#Temp%') 
    Drop Table #temp 
    Create Table #temp ([Id] int, [Name] varchar(50), [Age] int, [Sex] bit default 1) 
    Go 
    Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(1,'James',254,default) 
    Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(1,'James',274,default) 
    Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(1,'Jamessdf',22,default) 
    Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(2,'Lisa',245,0) 
    Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(2,'Lishja',24,0) 
    Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(2,'Lisghja',225,0) 
    Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(3,'Mirjsa',23,0) 
    Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(3,'Mirhjsa',278,0) 
    Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(3,'Mirghsa',23,0) 
    Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(4,'Joghjhn',26,default) 
    Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(5,'Abghjraham',28,default) 
    Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(6,'Linghjcoln',30,default) 
    
    Select * From #temp 
    --删除ID重复
    Delete T From 
    (Select Row_Number() Over(Partition By [ID] order By [ID]) As RowNumber,* From #Temp)T 
    Where T.RowNumber > 1 
    
    
    --------------------------删除多字段重复-----------------------------------------------
    
    TRUNCATE TABLE #temp --清空表的数据
    Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(1,'James',254,default) 
    Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(1,'James',274,default) 
    Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(1,'Jamessdf',22,default) 
    Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(2,'Lisa',245,0) 
    Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(2,'Lisa',24,0) 
    Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(2,'Lisghja',225,0) 
    Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(3,'Mirjsa',23,0) 
    Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(3,'Mirjsa',278,0) 
    Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(3,'Mirghsa',23,0) 
    Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(4,'Joghjhn',26,default) 
    Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(5,'Abghjraham',28,default) 
    Insert Into #temp ([Id] , [Name] , [Age] , [Sex] ) Values(6,'Linghjcoln',30,default) 
    
    --删除编号与名字 重复的数据  Partition By [ID],[Name]
    Delete T From 
    (Select Row_Number() Over(Partition By [ID],[Name] order By [ID]) As RowNumber,* From #Temp)T 
    Where T.RowNumber > 1 
  • 相关阅读:
    正则表达式体会
    checkbox、全选反选,获取值
    弹出窗体值回调
    页面点击任意js事件,触发360、IE浏览器新页面
    XML增、删、改
    面试题
    行列转换
    DataTable 和Json 字符串互转
    前台js与后台方法互调
    文件与base64二进制转换
  • 原文地址:https://www.cnblogs.com/suqifeng/p/2969412.html
Copyright © 2020-2023  润新知