T表
FILE1  FILE1  FILE1
1       2     1
2       3     1
3       4     1
4       4     3
5       4     5需要的结果FILE1  FILE1  FILE1
1       2     1
2       3     1
5       4     5
怎么写这个查询

解决方案 »

  1.   

    select * from t where file3=(select max(file3) from t t2 where t2.file2=t.file2)
      

  2.   

    select * from t where not exists(select 1 from t t2 where t2.file2=t.file2 and t2.file3<t.file3)
      

  3.   


    T表 
    FILE1  FILE1  FILE1 
    1       2     1 
    2       3     1 
    3       4     1 
    4       4     3 
    5       4     5 
    ---------------------
    字段一样?还是改下字段吧。T表 
    FILE1  FILE2  FILE3 
    1       2     1 
    2       3     1 
    3       4     1 
    4       4     3 
    5       4     5 select * from t a where not exists(select 1 from t where file2=a.file2 and file3>a.file3)
    --或者:
    select * from t a where file3 in (select max(file3) from t where file2=a.file2)
    --等等
      

  4.   

    create table T(FILE1 int,FILE2 int,FILE3 int)
    go
    insert into t
    select 1,2,1
    union all select 2,3,1
    union all select 3,4,1
    union all select 4,4,3
    union all select 5,4,5
    go
    select * from t
    select * from t where not exists(select 1 from t t2 where t2.file2=t.file2 and t2.file3 >t.file3)
    go
    drop table t;
    go
      

  5.   

    T表 
    FILE1  FILE2  FILE3
    1       2     1 
    2       3     1 
    3       4     1 
    4       4     3 
    5       4     5 需要的结果 FILE1  FILE2  FILE3 
    1       2     1 
    2       3     1 
    5       4     5 
    怎么写这个查询
    改字段为1,2,3
    select m.* from t m where file1 = (select max(file1) from t where file2 = m.file2)
      

  6.   

    对不起,字段的指不是一样的,实际效果是这样。
    FILE1  FILE2  FILE3 
    101      A     1  
    201      B     1  
    301      C     1  
    401      C     3  
    501      C     5  结果
    FILE1  FILE2  FILE3 
    101      A     1  
    201      B     1  501      C     5  
      

  7.   

    create table t(FILE1 varchar(20),FILE2 varchar(20),FILE3 int)
    go
    insert into t
    select '101','A',1  
    union all select '201','B',1  
    union all select '301','C',1  
    union all select '401','C',3  
    union all select '501','C',5  
    go
    select * from t;
    select * from t where not exists(select 1 from t t2 where t2.file2=t.file2 and t2.file3>t.file3)
    go
    drop table t;
    go
    /*
    结果
    FILE1  FILE2  FILE3 
    101      A     1  
    201      B     1  501      C     5  */(所影响的行数为 5 行)FILE1                FILE2                FILE3       
    -------------------- -------------------- ----------- 
    101                  A                    1
    201                  B                    1
    301                  C                    1
    401                  C                    3
    501                  C                    5(所影响的行数为 5 行)FILE1                FILE2                FILE3       
    -------------------- -------------------- ----------- 
    101                  A                    1
    201                  B                    1
    501                  C                    5(所影响的行数为 3 行)
      

  8.   


    --这样也可以
    select * 
    from t 
    where file3=(select max(file3) from t t2 where t2.file2=t.file2) 
    order by file1