易截截图软件、单文件、免安装、纯绿色、仅160KB

SQL 删除重复数据,只保留1条

if not object_id('Tempdb..#T') is null
    drop table #T
Go
Create table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))
Insert #T
select 1,N'A',N'A1' union all
select 2,N'A',N'A2' union all
select 3,N'A',N'A3' union all
select 4,N'B',N'B1' union all
select 5,N'B',N'B2'
Go
--I、Name相同ID最小的记录(推荐用1,2,3),保留最小一条
方法1:
delete a from #T a where  exists(select 1 from #T where Name=a.Name and ID<a.ID)
方法2:
delete a  from #T a left join (select min(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID where b.Id is null
方法3:
delete a from #T a where ID not in (select min(ID) from #T where Name=a.Name)
方法4(注:ID为唯一时可用):
delete a from #T a where ID not in(select min(ID)from #T group by Name)
方法5:
delete a from #T a where (select count(1) from #T where Name=a.Name and ID<a.ID)>0
方法6:
delete a from #T a where ID<>(select top 1 ID from #T where Name=a.name order by ID)
方法7:
delete a from #T a where ID>any(select ID from #T where Name=a.Name)
select * from #T
生成结果:
/*
ID          Name Memo
----------- ---- ----
1           A    A1
4           B    B1
(2 行受影响)
*/
--II、Name相同ID保留最大的一条记录:
方法1:
delete a from #T a where  exists(select 1 from #T where Name=a.Name and ID>a.ID)
方法2:
delete a  from #T a left join (select max(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID where b.Id is null
方法3:
delete a from #T a where ID not in (select max(ID) from #T where Name=a.Name)
方法4(注:ID为唯一时可用):
delete a from #T a where ID not in(select max(ID)from #T group by Name)
方法5:
delete a from #T a where (select count(1) from #T where Name=a.Name and ID>a.ID)>0
方法6:
delete a from #T a where ID<>(select top 1 ID fr


相关文档:

sql中and及or 的执行效率

应用中发现sql中的and及or的执行效率问题
sql语句,为什么把最后的or换成and,查询的就很快,使用的是mssql范例中northwind数据库为例,
select * from Orders a left join [Order Details] b on a.orderid = b.orderid
where a.customerid like '%ics%' or b.productid in (42,72)
追踪了语句的执行方案,发现 ......

SQL Server示例查询

Student(S#,Sname,Sage,Ssex) 学生表
Course(C#,Cname,T#) 课程表
SC(S#,C#,score) 成绩表
Teacher(T#,Tname) 教师表
问题:
1、查询“001”课程比“002”课程成绩高的所有学生的学号;
  select a.S# from (select s#,score from SC where C#='001') a,(select s#,score
  fr ......

转 SQL server 与Oracle开发比较

原文地址:http://www.cnblogs.com/wangxiaohuo/archive/2008/04/20/1162631.html
 
本文档主要从oracle与sql server语法上进行差异性比较,诸如两者在管理,性能,优化等方面的差异不作比较。
●概念上区别
   1.Oracle 是一种对象关系数据库管理系统(ORDBMS),而Sql server 只是关系型数据库管
&nbs ......

SQL Server日期操作

1、查询两个时间之间
select * from [tablename] where date between \'value1\' and \'value2\'
 
2、显示最后回复时间
select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
 
3、日程安排提前5分钟提醒
select * from 日程安排 w ......

sql语言:如何判断字段是否存在,如何删除及创建字段

如何判断字段是否存在
 if col_length('表名','字段1') is null  ALTER TABLE 表名 ADD 字段1 Nvarchar(50)  if col_length('表名','字段2') is null  ALTER TABLE 表名 ADD 字段2 Nvarchar(50) ");
删除字段
if col_length('表名','字段1,') is not null  ALTER TABLE 表名 drop  c ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号