你的表里只有日期、公司、工程、标号、价格这五个字段吗??如果是的话,就select distinct * from 表不是的话,就select distinct 日期,公司,工程,标号,价格 from 表
alter table tablename add sid int identity(1,1) select * from tablename a where exists(select 1 from tablename where 日期=a.日期 and 公司=a.公司 and 工程=a.工程 and 标号=a.标号 and 价格=a.价格 and id<a.id)alter table tablename drop column sid
select * into #t from (select distinct * from tablename)t truncate table tablename insert into tablename select * from #t
Decalre @日期 datetime, @公司 varchar(50), @工程 varchar(100), @标号 varchar(10), @价格 int, @i int set @日期='1920/10/01' set @公司 ='' set @工程 ='' set @标号='' set @价格 =0 set @i=0 select *,bb=0 into #b from 表 ----------把表放進臨時表-------- update #b set bb=@i,@i=(case when @日期=日期 and @公司=公司 and @工程=工程 and @标号=标号 and @价格=价格 then @i+1 else 1 end), @日期=日期,@公司=公司,@工程=工程, @标号=标号,@价格=价格 -----------------------------------保存唯一 delete from #b where bb<>1 --------------- truncate table 表 ----把表清空 insert into 表 select 日期,公司,工程,标号,价格 from #bdrop table #b ---------醫出來
create table t([name] nvarchar(10)) insert into t select 'ralph' union all select 'king' union all select 'ralph' union all select 'king' goalter table t add id int identity(1,1) go select * from t go delete t from t a where exists(select * from t where id<a.id and [name]=a.[name]) go select * from t go alter table t drop column id go drop table t go
select *
from tablename a
where exists(select 1 from tablename where 日期=a.日期 and 公司=a.公司 and 工程=a.工程
and 标号=a.标号 and 价格=a.价格 and id<a.id)alter table tablename drop column sid
truncate table tablename
insert into tablename select * from #t
@公司 varchar(50),
@工程 varchar(100),
@标号 varchar(10),
@价格 int,
@i int
set @日期='1920/10/01'
set @公司 =''
set @工程 =''
set @标号=''
set @价格 =0
set @i=0
select *,bb=0 into #b from 表
----------把表放進臨時表--------
update #b set bb=@i,@i=(case when @日期=日期 and @公司=公司 and @工程=工程 and
@标号=标号 and @价格=价格 then @i+1 else 1 end),
@日期=日期,@公司=公司,@工程=工程,
@标号=标号,@价格=价格
-----------------------------------保存唯一
delete from #b where bb<>1
---------------
truncate table 表 ----把表清空
insert into 表
select 日期,公司,工程,标号,价格 from #bdrop table #b ---------醫出來
-----是對的
insert into t select 'ralph' union all select 'king'
union all select 'ralph' union all select 'king'
goalter table t add id int identity(1,1)
go
select * from t
go
delete t
from t a
where exists(select * from t where id<a.id and [name]=a.[name])
go
select * from t
go
alter table t drop column id
go
drop table t
go