select 
    t.* 
from 
    表 t 
where 
    not exists(select 1 from 表 where str=t.str and key<t.key)
order by [t.time]

解决方案 »

  1.   

    select min(Key),str,min(time)
    from 表
    group by str
      

  2.   

    select 
        t.* 
    from 
        表 t 
    where 
        not exists(select 1 from 表 where [str]=t.[str] and [key]<t.[key])
    order by [t.time]
      

  3.   

    上面有个笔误:select 
        t.* 
    from 
        表 t 
    where 
        not exists(select 1 from 表 where [str]=t.[str] and [key]<t.[key])
    order by t.[time]
      

  4.   

    select 
      t.Key ,t.Str, t.Time
    from 
        表 t 
    where 
        Time=(select min(time) from 表 where t.Str=str)
      

  5.   

    select 1 from 表 where [str]=t.[str] and [key]<t.[key]
    一直是空啊
      

  6.   

    我在VS2003里面设计查询会把[Key]<t.[Key]优化成 [Key]<[key],于是永远是空
      

  7.   

    Select min(Key) as Key ,Str,min(Time) as Time
    from table1
    group by Str
    order by min(Time)不就行了?
      +-----------------------------------------------------+
      |        学无止境....
      +-----------------------------------------------------+
      

  8.   

    churchatp1(别看资料,看聊效!)  说的很对
    where [str]=t.[str] and [key]<t.[key]想想吧,你这条件永远不成立,就跟 1<1 是一样的效果取唯一直有两种办法 要么 distinct ,要么 group by   +-----------------------------------------------------+
      |        学无止境....
      +-----------------------------------------------------+
      

  9.   

    declare @t table(Key1 int,Str varchar(10),Time datetime)
    insert into @t select 1,'a','2005-11-1'
    union all select 2,'a','2005-11-2'
    union all select 3,'b','2005-11-2'
    union all select 4,'b','2005-11-3'select *  from @t t where not exists (select * from @t b where t.str=b.str and t.time>b.time)/*Key1        Str        Time                                                   
    ----------- ---------- ------------------------------------------------------ 
    1           a          2005-11-01 00:00:00.000
    3           b          2005-11-02 00:00:00.000 */
    你可以这样理解
    for str,time in (select * from @t a ) 
         if ( not exists ( select * from @t b where a.str=b.str and a.time>b.time ) ) then
                OUTPUT
         end if;
    next你可以把str,time的数据代到循环中就明白了
    第一次循环a,'2005-11-1'  not exists ( select * from @t b where a=b.str and 2005-11-1>b.time )
    第二次循环a,'2005-11-2'  not exists ( select * from @t b where a=b.str and 2005-11-2<b.c )
    ....
    第N次循环.....
      

  10.   

    上面笔误
    declare @t table(Key1 int,Str varchar(10),Time datetime)
    insert into @t select 1,'a','2005-11-1'
    union all select 2,'a','2005-11-2'
    union all select 3,'b','2005-11-2'
    union all select 4,'b','2005-11-3'select *  from @t t where not exists (select * from @t b where t.str=b.str and t.time>b.time)/*Key1        Str        Time                                                   
    ----------- ---------- ------------------------------------------------------ 
    1           a          2005-11-01 00:00:00.000
    3           b          2005-11-02 00:00:00.000 */
    你可以这样理解
    for str,time in (select * from @t a ) 
         if ( not exists ( select * from @t b where a.str=b.str and a.time>b.time ) ) then
                OUTPUT
         end if;
    next你可以把str,time的数据代到循环中就明白了
    第一次循环a,'2005-11-1'  not exists ( select * from @t b where a=b.str and 2005-11-1>b.time )
    第二次循环a,'2005-11-2'  not exists ( select * from @t b where a=b.str and 2005-11-2<b.time )
    ....
    第N次循环.....
      

  11.   

    晕死了上面又打错字了
    上面笔误
    declare @t table(Key1 int,Str varchar(10),Time datetime)
    insert into @t select 1,'a','2005-11-1'
    union all select 2,'a','2005-11-2'
    union all select 3,'b','2005-11-2'
    union all select 4,'b','2005-11-3'select *  from @t t where not exists (select * from @t b where t.str=b.str and t.time>b.time)/*Key1        Str        Time                                                   
    ----------- ---------- ------------------------------------------------------ 
    1           a          2005-11-01 00:00:00.000
    3           b          2005-11-02 00:00:00.000 */
    你可以这样理解
    for str,time in (select * from @t a ) 
         if ( not exists ( select * from @t b where a.str=b.str and a.time>b.time ) ) then
                OUTPUT
         end if;
    next你可以把str,time的数据代到循环中就明白了
    第一次循环a,'2005-11-1'  not exists ( select * from @t b where a=b.str and 2005-11-1>b.time )
    第二次循环a,'2005-11-2'  not exists ( select * from @t b where a=b.str and 2005-11-2>b.time )
    ....
    第N次循环.....