SQL2005中的分组统计问题

来源:百度知道 编辑:UC知道 时间:2024/05/22 23:47:19
目前有个表personnel ,其中有列列名为 '姓名' ,我想把 姓氏唯一的人名都列出来,语句该怎么写呢? 请大家指点
select 姓名 from id not in(select id from personnel as a,personnel as b where a.id<>b.id and a.姓名=b.姓名)

系统提示 关键字 'not' 附近有语法错误。

哦,如果不知道主键呢?

nomanland的答案太深了 有点看不懂 可能我过于愚钝了
沉默用户的答案 一下就实现我想要的

多谢2位

select 姓名
from personnel a,(select left(姓名,1) as 姓 from personnel group by left(姓名,1) having count(*)=1) b
where left(a.姓名,1)=b.姓

假设主键为id

select 姓名 from id not in(select id from personnel as a,personnel as b where a.id<>b.id and a.姓名=b.姓名)

-----------------------------------------
select 姓名 from 表 where id not in(select id from personnel as a,personnel as b where a.id<>b.id and substring(a.姓名,1,2)=substring(b.姓名,1,2))
中文情况下的单姓

你最好把姓和名分两列,不然复姓的情况考虑起来蛮麻烦的