读书人

一个筛选有关问题

发布时间: 2013-09-07 14:12:44 作者: rapoo

一个筛选问题
有表如下:
field1field2field3
三5A
三B
三56C
三4A
李四8T
李四5E
李四2
李四3E
李四4Y
李四7Y
王五8T
王五8H
王五4G
六2J
六34E
六5Y
六6U
六4H
六8B
六7F
六8R
请问如何可以选出field2,field3都不是空的人,并且distinct出来人名?
[解决办法]

create table #tb(field1 varchar(10),field2 varchar(10),field3 varchar(10))
insert into #tb
select '三','5','A'
union all select '三',null,'B'
union all select '三',56,'C'
union all select '三',4,'A'
union all select '李四',8,'T'
union all select '李四',5,'E'
union all select '李四',2,null
union all select '李四',3,'E'
union all select '李四',4,'Y'
union all select '李四',7,'Y'
union all select '王五',8,'T'
union all select '王五',8,'H'
union all select '王五',4,'G'
union all select '六',2,'J'
union all select '六',34,'E'
union all select '六',5,'Y'
union all select '六',6,'U'
union all select '六',4,'H'
union all select '六',8,'B'
union all select '六',7,'F'
union all select '六',8,'R'

select distinct field1
from #tb
where field1 not in(select field1 from #tb where isnull(field1,'')='' or isnull(field2,'')='')

drop table #tb


/*
李四
王五


*/

读书人网 >SQL Server

热点推荐