有这么一个表: 
tblStud(fId, fName, fAge, fClass) 说明:fId是主键,唯一编号,fName有可能重复,fAge, fClass只是表的一些其它信息.
由于fName 可能重复, 因此,我想得到这样一个表,四个字段都有,但姓名不重复, 对于姓名重复的记录,只取任意一条就可以了. 示例数据如下:
fId     fName  fAge    fClass
1 李军 21 大二
2 张红 21 大三
3 张三 20 大一
4 李军 22 大三
5 王五 20 大三
6 张红 22 大三需要得到的数据如下:fId     fName  fAge    fClass
1 李军 21 大二
2 张红 21 大三
3 张三 20 大一
5 王五 20 大三"4"和"6"已经重复,因此不要.自己的想法是:应该对fName 进行分组查询,可是用分组语句,只能得到"fName"这一列,其它的三个值没法得到,
试着用"top 1"也不行. 
后来想着用: select * from tblStud where fId in( select fId from tblStud group by fName ) 呵呵,这条语句明显有问题,却不知道怎样解决.. 
想着用distinct 仍然不行..不知各位是怎样解决的? 最好能说说你们都是怎样分析的..

解决方案 »

  1.   

    use aggregate function,select * from tblSud Where fId in (select min(fId) from tblStud group by fName)
      

  2.   

    哎,明天再去试试吧.一直没有用min 和max 去试. 因为上面的只是随意构造的一组数据,
    在表里面,fId 是一个varchar 类型的. 当时,只是想着,min max 只能用在数值型里,所有连试都没有试..
      

  3.   

    --任意选一条
    select * from tblStud a where fId=(select top 1 fId from tblStud where fName=a.fName order by newid())
    --取最大或最小
    select * from tblStud a where fId=(select min/*max*/(fId) from tblStud where fName=a.fName)
      

  4.   

    select fID, fName, fAge, fClass
    from tblStud
    where fID = (select top 1 fID
                 from tblStud
                 where fName = A.fName)
      

  5.   

    --按某一字段分组取最大(小)值所在行的数据
    --2007-10-23于杭州
    /*
    数据如下:
    name val memo
    a    2   a2(a的第二个值)
    a    1   a1--a的第一个值
    a    3   a3:a的第三个值
    b    1   b1--b的第一个值
    b    3   b3:b的第三个值
    b    2   b2b2b2b2
    b    4   b4b4
    b    5   b5b5b5b5b5
    */
    --创建表并插入数据:
    create table tb(name varchar(10),val int,memo varchar(20))
    insert into tb values('a',    2,   'a2(a的第二个值)')
    insert into tb values('a',    1,   'a1--a的第一个值')
    insert into tb values('a',    3,   'a3:a的第三个值')
    insert into tb values('b',    1,   'b1--b的第一个值')
    insert into tb values('b',    3,   'b3:b的第三个值')
    insert into tb values('b',    2,   'b2b2b2b2')
    insert into tb values('b',    4,   'b4b4')
    insert into tb values('b',    5,   'b5b5b5b5b5')
    go--一、按name分组取val最大的值所在行的数据。
    --方法1:
    select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
    --方法2:
    select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
    --方法3:
    select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
    --方法4:
    select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
    --方法5
    select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          3           a3:a的第三个值
    b          5           b5b5b5b5b5
    */--二、按name分组取val最小的值所在行的数据。
    --方法1:
    select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name
    --方法2:
    select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)
    --方法3:
    select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
    --方法4:
    select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
    --方法5
    select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          1           a1--a的第一个值
    b          1           b1--b的第一个值
    */--三、按name分组取第一次出现的行所在的数据。
    select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          2           a2(a的第二个值)
    b          1           b1--b的第一个值
    */--四、按name分组随机取一条数据。
    select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          1           a1--a的第一个值
    b          5           b5b5b5b5b5
    */--五、按name分组取最小的两个(N个)val
    select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.val
    select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.val
    select a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          1           a1--a的第一个值
    a          2           a2(a的第二个值)
    b          1           b1--b的第一个值
    b          2           b2b2b2b2
    */--六、按name分组取最大的两个(N个)val
    select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val
    select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.val
    select a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          2           a2(a的第二个值)
    a          3           a3:a的第三个值
    b          4           b4b4
    b          5           b5b5b5b5b5
    */
      

  6.   

    create table tblStud(fId int,fName varchar(20),fAge int,fClass varchar(20))
    insert into tblStud values(1, '李军',21,  '大二 ')
    insert into tblStud values(2, '张红', 21, '大三')
    insert into tblStud values(3, '张三', 20, '大一 ')
    insert into tblStud values(4, '李军',22,  '大三 ')
    insert into tblStud values(5,  '王五',20, '大三 ')
    insert into tblStud values(6,  '张红',22, '大三 ')
    drop table tblStudselect * from tblStud a where fId=(select top 1 fId from tblStud where fName=a.fName order by newid())
      

  7.   

    谢谢各位了.看了各位的回复,真有点惭愧...看了各位的代码, 其中有随机提取的地方. 比如:--任意选一条
    select * from tblStud a where fId=(select top 1 fId from tblStud where fName=a.fName order by newid())查看SQL的帮助,"说明  对于每台计算机,由 NEWID 返回的值不同。所显示的数字仅起解释说明的作用。" 
    newid() 这个函数有点类似于其它编程语言里的: GUID 也就是唯一的全局变量吧.select top 1 * from tblStud where fClass = '大一' order by newid()这条语语确实产生了随机数,可是对于它是怎样产生的,有点不明白. 不明白 order by newid() 它是怎样处理的,因为对于order by 的理解是,它只对字段进行排序而已...类似于这样的语句:select top 1 * from tblStud where fClass = '大一' order 'hello' /*始终返回的第一行*/
    换成"order by 123 " 会提示,超出了123行记录..