CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

怎么删除多余的数据?

楼主freshman(新手)2003-12-01 15:50:35 在 MS-SQL Server / 基础类 提问

表A  
  id     name  
  1         a  
  2         a  
  3         c  
  4         c  
  5         c  
  6         d  
   
  怎么删除,以变成  
  id     name  
  1         a  
  3         c  
  6         d  
   
   
  谢谢! 问题点数:0、回复次数:6Top

1 楼sdhdy(大江东去...)回复于 2003-12-01 15:54:19 得分 0

delete   a   where   id   not   in   (select   min(id)   from   a   group   by   name)Top

2 楼wzh1215(懒猫)回复于 2003-12-01 15:56:37 得分 0

delete   from   表A  
  where   id   not   in   (select   min(id)   from   表A   group   by   name)Top

3 楼CrazyFor(冬眠的鼹鼠)回复于 2003-12-01 15:57:13 得分 0

如果有ID字段,就是具有唯一性的字段  
   
  delect   table   where   id   not   in   (  
   
      select   max(id)   from   table   group   by   col1,col2,col3...  
  )  
  group   by   子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。  
   
   
   
  2,如果是判断所有字段也可以这样  
      select   *   into   #aa   from   table   group   by   id1,id2,....  
      delete   table    
      insert   into   table    
      select   *   from   #aa  
   
   
   
  3,没有ID的情况  
   
  select   identity(int,1,1)   as   id,*   into   #temp   from   tabel  
  delect   #   where   id   not   in   (  
      select   max(id)   from   #   group   by   col1,col2,col3...)  
  delect   table  
  inset   into   table(...)  
        select   .....   from   #temp  
   
   
  col1+','+col2+','...col5   联合主键  
   
   
  select   *   from     table   where   col1+','+col2+','...col5   in   (  
   
      select   max(col1+','+col2+','...col5)   from   table    
  where   having   count(*)>1  
  group   by   col1,col2,col3,col4    
  )  
  group   by   子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。  
   
  2,  
  select   identity(int,1,1)   as   id,*   into   #temp   from   tabel  
  select   *   from     #temp   where   id   in   (  
      select   max(id)   from   #emp   where   having   count(*)>1   group   by   col1,col2,col3...)  
  Top

4 楼lvltt(未完成)回复于 2003-12-01 16:12:08 得分 0

delete   a   from   表A   where   id   not   in   (select   min(id)   from   a   group   by   name)  
  Top

5 楼liuxiaoyuzhou(蟀哥)回复于 2003-12-01 18:17:50 得分 0

select   distinct   name   ,id  
  form表名  
   
  解决!Top

6 楼dlpseeyou(豆子)回复于 2003-12-01 18:21:16 得分 0

select   distinct(name)   ,id   form   表名    
  Top

相关问题

  • 没有主键的表,数据导了两遍,怎么删除多余的数据?
  • aspxgrid怎么删除数据?
  • 如何将表中重复的多余数据删除
  • 数据库被删除了怎么救~~~~~~~~~~~~~
  • 删除的数据库怎么还原?
  • 怎么删除DBF里的数据?
  • 挑战高手:SQL Server 2000, 一个表, 包含完全相同重复数据若干,怎么删除多余记录?
  • 如何删除一个application里面多余的窗口或者数据窗口?
  • 删除数据库里冗余的上传文件,关键字(Fso,删除,多余文件)
  • 怎么样删除多余的TOOLBAR按钮?

关键词

  • 字段
  • col
  • group
  • 相同
  • 判断
  • max
  • where
  • delete
  • min
  • temp

得分解答快速导航

  • 帖主:freshman

相关链接

  • SQL Server类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo