university                   picture         groups
         北京大学光华管理学院  01-1.jpg   1
北京大学光华管理学院  01-2.jpg   1
北京大学光华管理学院  01-3.jpg   1
北京大学光华管理学院  01-4.jpg   1
清华大学经济管理学院  02-1.jpg   2
清华大学经济管理学院  02-2.jpg   2
         清华大学经济管理学院         02-3.jpg         2  
         复旦大学管理学院             03-1.jpg         3
         复旦大学管理学院             03-2.jpg         3
         复旦大学管理学院             03-3.jpg         3
想通过一sql语句,得到这样的结果
university                  picture          groups
北京大学光华管理学院  01-1.jpg   1
清华大学经济管理学院  02-1.jpg   2
复旦大学管理学院             03-1.jpg         3也就是说按groups的递增,依次取出每个group中的第一条记录。
对sql语句较熟的朋友,望赐教!

解决方案 »

  1.   

    university                       picture               groups  
               北京大学光华管理学院              01-1.jpg                1              
               北京大学光华管理学院              01-2.jpg                1              
               北京大学光华管理学院              01-3.jpg                1              
               北京大学光华管理学院              01-4.jpg                1              
               清华大学经济管理学院              02-1.jpg                2              
               清华大学经济管理学院              02-2.jpg                2              
               清华大学经济管理学院              02-3.jpg                2      
               复旦大学管理学院                  03-1.jpg                3  
               复旦大学管理学院                  03-2.jpg                3  
               复旦大学管理学院                  03-3.jpg                3  
    想通过一sql语句,得到这样的结果  
    university                        picture                 groups  
    北京大学光华管理学院              01-1.jpg                1  
    清华大学经济管理学院              02-1.jpg                2  
    复旦大学管理学院                  03-1.jpg                3  
     
    也就是说,随着groups的递增,依次取出每个group中的第一条记录。  
    对sql语句较熟的朋友,望赐教!
      

  2.   

    create table Table1 (university varchar(50),picture varchar(20),groups int)
    insert into table1
    select '北京大学光华管理学院 ','01-1.jpg',    1 
    union all
    select '北京大学光华管理学院 ','01-2.jpg',    1 
    union all
    select '北京大学光华管理学院 ','01-3.jpg',    1 
    union all
    select '北京大学光华管理学院 ','01-4.jpg',    1 
    union all
    select '清华大学经济管理学院 ','02-1.jpg',    2 
    union all
    select '清华大学经济管理学院 ','02-2.jpg',    2 
    union all
    select '清华大学经济管理学院 ','02-3.jpg',    2 
    union all
    select '复旦大学管理学院 ','03-1.jpg',    3 
    union all
    select '复旦大学管理学院 ','03-2.jpg',    3 
    union all
    select '复旦大学管理学院 ','03-3.jpg',    3 
    --------
    select * ,identity(int,1,1) as ID into #t from table1--利用中间表
    --------
    --Try It
    select A.university,A.Picture,A.groups from #t A
    where not exists
    (select * from #t where university=A.university and groups=A.groups and ID<A.id)
    ---
    Drop table #t
    Drop table Table1
      

  3.   

    create table tb (university varchar(50),picture varchar(20),groups int)
    insert into tb
    select '北京大学光华管理学院 ','01-1.jpg',    1 
    union all
    select '北京大学光华管理学院 ','01-2.jpg',    1 
    union all
    select '北京大学光华管理学院 ','01-3.jpg',    1 
    union all
    select '北京大学光华管理学院 ','01-4.jpg',    1 
    union all
    select '清华大学经济管理学院 ','02-1.jpg',    2 
    union all
    select '清华大学经济管理学院 ','02-2.jpg',    2 
    union all
    select '清华大学经济管理学院 ','02-3.jpg',    2 
    union all
    select '复旦大学管理学院 ','03-1.jpg',    3 
    union all
    select '复旦大学管理学院 ','03-2.jpg',    3 
    union all
    select '复旦大学管理学院 ','03-3.jpg',    3 select * 
    from tb tm 
    where not exists(select 1 from tb where tm.groups = tb.groups and cast(substring(tm.picture,charindex('.',tm.picture)-1,1) as int)>cast(substring(tb.picture,charindex('.',tb.picture)-1,1)  as int))drop table tb
    /*
    university                                         picture              groups      
    -------------------------------------------------- -------------------- ----------- 
    北京大学光华管理学院                                         01-1.jpg             1
    清华大学经济管理学院                                         02-1.jpg             2
    复旦大学管理学院                                           03-1.jpg             3(所影响的行数为 3 行)
    */
      

  4.   

    select 
      university,min(picture) as picture,groups
    from 
      table1
    group by 
      university,groups
    order by 
      groups
      

  5.   

    可以用like 将图片的格式限制成*-1.jpg 就好了
      

  6.   

    select *
    from table1
    where picture like '%#_01.jpg' ESCAPE '#'
      

  7.   

    select * from tablename a
    where picture=(select min(picture) from tablename where groups=a.groups)
      

  8.   


    declare @t table(university varchar(8000), picture varchar(8000), groups int)
    insert into @t
    select '北京大学光华管理学院',   '01-1.jpg',  1    union
    select '北京大学光华管理学院',  '01-2.jpg',  1   union
    select '北京大学光华管理学院',  '01-3.jpg',  1   union
    select '北京大学光华管理学院',   '01-4.jpg',  1    union
    select '清华大学经济管理学院',   '02-1.jpg',  2    union
    select '清华大学经济管理学院',  '02-2.jpg',  2   union
    select '清华大学经济管理学院',    '02-3.jpg',  2    union
    select '复旦大学管理学院',  '03-1.jpg',  3 union
    select '复旦大学管理学院',  '03-2.jpg',  3 union
    select '复旦大学管理学院',  '03-3.jpg',  3select university , min(picture) as picture, groups
    from @t
    group by university ,groups 
    order by groups
      

  9.   

    Haiwer(海阔天空) ( )的语句我有些看不懂
    picture=(select min(picture) from tablename where groups=a.groups)
    没有grouy by 也可以分组?
    还有啊 picture中的等于号只能查一条语句,那个怎么能查出三条呢?
    请高人指点~
      

  10.   

    where picture=(select min(picture) from tablename where groups=a.groups)這是一個條件,每條紀錄都去判斷是否滿足這個條件。排除下來,有3條紀錄滿足條件。
      

  11.   

    可以这样理解吗
    第一条记录 北京大学光华管理学院 01-1.jpg  1
    首先拿出第一条记录,然后
    select min(picture) from tablename where groups=a.groups
    这句话是把groups=1的所有都找出来在找出最小的picture值也就是第一条
    然后picture=01-1.jpg 
    下面的第二条
    北京大学光华管理学院 01-2.jpg  1
    再如上比较再得出picture=01-1.jpg
    那不是有多少条记录就要返回都少吗?
    迷茫中~~
      

  12.   

    因为第二条记录也满足
    select min(picture) from tablename where groups=a.groups
    中的groups=a.groups啊因为都是1
    所以会不会仍然选出各最小的来呢
    不能理解它的执行过程
      

  13.   

    多谢大家了,问题解决了,特别是Haiwer(海阔天空) ,谢谢!