是的,不支持差运算,可以使用
对于完全相同的远组,可以通过distinct
对于不完全相同的,两种方法,一种是group by 然后对不同的字段加统计函数
另一种是采用语句。
/*-----------实例-------------*/
declare @s table
(
Productname varchar(20),price int,[date] smalldatetime)insert @s values ('电脑',100,'2005-6-26')
insert @s values ('手机',110,'2005-6-27')
insert @s values ('电脑',140,'2006-1-1')
insert @s values ('电脑',120,'2005-6-27')
insert @s values ('手机',150,'2005-6-28')
insert @s values ('摩托',140,'2005-6-26')select * from @sProductname price date
-------------------- ----------- ------------------------------------------------------
电脑 100 2005-06-26 00:00:00
手机 110 2005-06-27 00:00:00
电脑 140 2006-01-01 00:00:00
电脑 120 2005-06-27 00:00:00
手机 150 2005-06-28 00:00:00
摩托 140 2005-06-26 00:00:00(所影响的行数为 6 行)
比如这样一个数据集,可以通过
/*--------------求Productname相同,price最大者---------------*/
select * from @s as a where not exists (select * from @s as b where a.Productname = b.Productname and a.price<b.price)/*-----------------或者------------------*/
select * from @s as a where (select count(1) from @s as b where a.Productname = b.Productname and a.price<b.price)<1
/*----------------结果-------------------*/
Productname price date
-------------------- ----------- ------------------------------------------------------
电脑 140 2006-01-01 00:00:00
手机 150 2005-06-28 00:00:00
摩托 140 2005-06-26 00:00:00(所影响的行数为 3 行)
对于完全相同的远组,可以通过distinct
对于不完全相同的,两种方法,一种是group by 然后对不同的字段加统计函数
另一种是采用语句。
/*-----------实例-------------*/
declare @s table
(
Productname varchar(20),price int,[date] smalldatetime)insert @s values ('电脑',100,'2005-6-26')
insert @s values ('手机',110,'2005-6-27')
insert @s values ('电脑',140,'2006-1-1')
insert @s values ('电脑',120,'2005-6-27')
insert @s values ('手机',150,'2005-6-28')
insert @s values ('摩托',140,'2005-6-26')select * from @sProductname price date
-------------------- ----------- ------------------------------------------------------
电脑 100 2005-06-26 00:00:00
手机 110 2005-06-27 00:00:00
电脑 140 2006-01-01 00:00:00
电脑 120 2005-06-27 00:00:00
手机 150 2005-06-28 00:00:00
摩托 140 2005-06-26 00:00:00(所影响的行数为 6 行)
比如这样一个数据集,可以通过
/*--------------求Productname相同,price最大者---------------*/
select * from @s as a where not exists (select * from @s as b where a.Productname = b.Productname and a.price<b.price)/*-----------------或者------------------*/
select * from @s as a where (select count(1) from @s as b where a.Productname = b.Productname and a.price<b.price)<1
/*----------------结果-------------------*/
Productname price date
-------------------- ----------- ------------------------------------------------------
电脑 140 2006-01-01 00:00:00
手机 150 2005-06-28 00:00:00
摩托 140 2005-06-26 00:00:00(所影响的行数为 3 行)
比如表是{1,2,2,3,3,3,4,4,4,4}
我想删掉所有出现的元组一次,也就是变成{2,3,3,4,4,4}
没有except应该怎么办?
比如表是{1,2,2,3,3,3,4,4,4,4}*/
create table tb(id int)
insert tb
select 1
union all select 2
union all select 2
union all select 3
union all select 3
union all select 3
union all select 4
union all select 4
union all select 4
union all select 4
alter table tb add sid int identity(1,1)
go
delete a from tb a where not exists(select 1 from tb where id=a.id and sid<a.sid)
alter table tb drop column sid
go
select * from tb
不好意思,初学~
alter table tb add sid int identity(1,1)
delete a from tb a where not exists(select 1 from tb where id=a.id and sid<a.sid)
alter table tb drop column sid
这三句放到存储过程中会提示'sid'无效~
该怎么处理呢?~