sql 去除重复记录的语句
表a:
id,name
表b:
id,aid,value
select case when a.name='ccc' then null else a.name end name,b.value from 表a a,表b b where a.id=b.aid
select nullif(a.name,'ccc') name ,b.value from 表a a,表b b where a.id=b.aid
生成测试数据表: [tb]
if object_id('[tb]') is not null
drop table [tb]
go
create table [tb] ([name] [nvarchar](10),[value] [int])
insert into [tb]
select 'aaa','1' union all
select 'bbb','2' union all
select 'ccc','3' union all
select 'ccc','4' union all
select 'ccc','5'
-->sql查询如下:
select name = case [value]
when (
select min([value])
from tb
where name = t.name
) then name
else ''
end, [value]
from [tb] t
/*
name value
---------- -----------
aaa 1
bbb 2
ccc 3
4
5
(5 行受影响)