数据应如下所示
Guid        wtGuid       mainguid       date
11          11_1          1111        2013-03-28 10:00:00.000
22          11_1          1111        2013-03-28 11:00:00.000
333         22_1          1111        2013-03-28 12:00:00.000
44          22_1          1111        2013-03-28 12:10:00.000
555         22_22         1111        2013-03-28 12:11:00.000得到的数据应是:
guid是 22、44、555 的数据
比如guid 是 11 和 22的数据 因为 wtGuid字段是同一个吗! 所以取 date最新的 就是 guid是22的数据的!!! 

解决方案 »

  1.   

    得到的数据应是:
    guid是 22、44、555 的数据

    比如guid 是 11 和 22的数据 因为 wtGuid字段是同一个吗! 所以取 date最新的 就是 guid是22的数据的!!!  
      

  2.   

    ----------------------------------------------------------------
    -- Author  :DBA_Huangzj(發糞塗牆)
    -- Date    :2013-04-01 16:28:22
    -- Version:
    --      Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (X64) 
    -- Jun 17 2011 00:54:03 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1, v.721)
    --
    ----------------------------------------------------------------
    --> 测试数据:[huang]
    if object_id('[huang]') is not null drop table [huang]
    go 
    create table [huang]([Guid] int,[wtGuid] varchar(5),[mainguid] int,[date] datetime)
    insert [huang]
    select 11,'11_1',1111,'2013-03-28 10:00:00.000' union all
    select 22,'11_1',1111,'2013-03-28 11:00:00.000' union all
    select 333,'22_1',1111,'2013-03-28 12:00:00.000' union all
    select 44,'22_1',1111,'2013-03-28 12:10:00.000' union all
    select 555,'22_22',1111,'2013-03-28 12:11:00.000'
    --------------开始查询--------------------------select * from [huang] a WHERE EXISTS (SELECT 1 FROM (SELECT MAX([guid])[guid],[wtGuid] FROM huang GROUP BY [wtGuid] ) b WHERE a.[guid]=b.[guid] AND a.[wtGuid]=b.[wtGuid])
    ----------------结果----------------------------
    /* 
    Guid        wtGuid mainguid    date
    ----------- ------ ----------- -----------------------
    22          11_1   1111        2013-03-28 11:00:00.000
    333         22_1   1111        2013-03-28 12:00:00.000
    555         22_22  1111        2013-03-28 12:11:00.000
    */