ID ProjectID Version ProjectName
1  1            1          P1
2  1            2          P1
3  1            3          P1
4  2            1          P2
5  2            2          P2
6  3            1          P3
我想要的结果是不同ProjectID中Version最大的那些数据:
ID ProjectID Version ProjectName
3  1            3          P1
5  2            2          P2
6  3            1          P3

解决方案 »

  1.   

    select 
      * 
    from 
      tb  t 
    where 
      not exists(select 1 from tb where ProjectID=t.ProjectID and Version>t.Version)
      

  2.   

    select * from tb t
    where not exists
    (
        select 1 from tb where t.ProjectID=ProjectId and t.ProjectName=ProjectName and
        p.Version<Version
    )
      

  3.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2009-10-25 21:37:23
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([ID] int,[ProjectID] int,[Version] int,[ProjectName] varchar(2))
    insert [tb]
    select 1,1,1,'P1' union all
    select 2,1,2,'P1' union all
    select 3,1,3,'P1' union all
    select 4,2,1,'P2' union all
    select 5,2,2,'P2' union all
    select 6,3,1,'P3'
    --------------开始查询--------------------------
    select 
      * 
    from 
      tb  t 
    where 
      not exists(select 1 from tb where ProjectID=t.ProjectID and Version>t.Version)
    ----------------结果----------------------------
    /*ID          ProjectID   Version     ProjectName
    ----------- ----------- ----------- -----------
    3           1           3           P1
    5           2           2           P2
    6           3           1           P3(3 行受影响)
     
    */
      

  4.   


    select *from tb t
    where not exists(select *from tb where ProjectID=t.ProjectID and Version>t.Version)
      

  5.   

    --> 测试数据:@tb
    declare @tb table([ID] int,[ProjectID] int,[Version] int,[ProjectName] varchar(2))
    insert @tb
    select 1,1,1,'P1' union all
    select 2,1,2,'P1' union all
    select 3,1,3,'P1' union all
    select 4,2,1,'P2' union all
    select 5,2,2,'P2' union all
    select 6,3,1,'P3'select * from @tb t where not exists(select 1 from @tb where ProjectID=t.ProjectID and Version>t.Version)select * from @tb  t where Version=(select max(Version) from @tb where ProjectID=t.ProjectID) order by id/*
    ID          ProjectID   Version     ProjectName
    ----------- ----------- ----------- -----------
    3           1           3           P1
    5           2           2           P2
    6           3           1           P3
    */
      

  6.   

    http://topic.csdn.net/u/20080626/00/43d0d10c-28f1-418d-a05b-663880da278a.html?85256
      

  7.   

    select 

    from 
    表名 as  a 
    where not exists
    (select 1 from 表名 where ProjectID=a.ProjectID and Version>a.Version)
      

  8.   

    select * from tb t 
    where not exists 

        select 1 from tb where t.ProjectID=ProjectId and t.ProjectName=ProjectName and 
        p.Version <Version 
    )
      

  9.   


    declare @tb table([ID] int,[ProjectID] int,[Version] int,[ProjectName] varchar(2))
    insert @tb
    select 1,1,1,'P1' union all
    select 2,1,2,'P1' union all
    select 3,1,3,'P1' union all
    select 4,2,1,'P2' union all
    select 5,2,2,'P2' union all
    select 6,3,1,'P3'select * from @tb a where 
    not exists(select 1 from @tb where a.[ProjectName] = [ProjectName] and id > a.id )
    ---------------------------
    ID          ProjectID   Version     ProjectName
    ----------- ----------- ----------- -----------
    3           1           3           P1
    5           2           2           P2
    6           3           1           P3(3 行受影响)
      

  10.   


    declare @tb table([ID] int,[ProjectID] int,[Version] int,[ProjectName] varchar(2))
    insert @tb
    select 1,1,1,'P1' union all
    select 2,1,2,'P1' union all
    select 3,1,3,'P1' union all
    select 4,2,1,'P2' union all
    select 5,2,2,'P2' union all
    select 6,3,1,'P3'select * from @tb a where id in
    (select max(id) from @tb where a.[ProjectName] = [ProjectName])
    order by id 
    -------------------------------------------
    ID          ProjectID   Version     ProjectName
    ----------- ----------- ----------- -----------
    3           1           3           P1
    5           2           2           P2
    6           3           1           P3(3 行受影响)