注册 登录
编程论坛 SQL Server论坛

请找出同一姓名有不同状态的记录明细

exvba 发布于 2010-08-13 10:55, 797 次点击
表名称:xm

姓名    编号    状态
a    101    好
a    101    中
b    102    中
a    101    好
c    103    中
b    102    差
d    104    好
d    104    好
e    105    无
e    106    中
b    102    好
   以上表中同一个姓名存在只有一种状态、2种状态以上的记录,请找出同一姓名存在2种状态(好、中、差、无)以上的所有记录,本例查询结果为姓名是a、b、e的8条记录。谢谢!
5 回复
#2
SQLCenter2010-08-13 12:28
select * from xm as t where (select count(distinct 状态) from xm where 姓名=t.姓名) >=2
#3
qingshuiliu2010-08-13 12:30
select * from  xm
where xm.姓名  in (select tb.姓名 from
(select xm.姓名,count(distinct xm.状态) as statusCount from xm group by 姓名) as tb
where tb.statusCount>1)



[ 本帖最后由 qingshuiliu 于 2010-8-13 12:38 编辑 ]
#4
exvba2010-08-13 14:21
感谢SQLCenter 和qingshuiliu两位老师。
#5
exvba2010-08-13 16:05
因本人积分不多,不好意思了
#6
changyawei2010-09-01 09:59
select t1.姓名 ,count(distinct t1.状态) from xm t1 group by t1.姓名 having count(distinct t1.状态) >=2
1