A     B     C
200   A1    F
300   A2    F
400   A3    D
100   A4    D现在我想给SQL条件当C字段等于F的时候。。找出A字段最大的值所对应的B字段是多少!
请问这个SQL语句该怎么写

解决方案 »

  1.   

    declare @t table(A int,B varchar(10),C varchar(10))
    insert @t
    select 200,   'A1',    'F' union all
    select 300,   'A2',    'F' union all
    select 400,   'A3',    'D' union all
    select 100,   'A4',    'D'select B from @t  where C = 'F' and  A = (select max(A) from @t where C = 'F')
      

  2.   

    select t.* from tablename t,(select max(a)a,c from tablename where c='f' group by c) x where t.a=x.a and t.c=x.c
      

  3.   

    为什么sql中没有first啊hoho 有了first 就要middle,然后就是second.....   ^_^
      

  4.   

    if ((select count(*) from sysobjects where name = 'table1') > 0)
        drop table table1
    create table table1(A int, B varchar(20), C varchar(20))
    insert into table1
    select 200, 'A1', 'F' union
    select 300, 'A3', 'F' union
    select 400, 'A3', 'D' union
    select 100, 'A4', 'D'
    select B from table1 where A = (select max(A)  from table1 where C = 'F')