请问如何筛选出每个LOTID下CleanTime的一笔资料?
谢谢

解决方案 »

  1.   


    select * from
    (select *,row_number() over(partition by LOTID order by CleanTime) 'rn' 
     from [表名]
    ) t where rn=1
      

  2.   

     with cte as(
     select row_number()over(partition by LOTID order by getdate())as xh
    ,....
    )
    select * from cte where xh=1
      

  3.   

    我这个是选出最大的cleantimeSELECT  *
    FROM    tab a
    WHERE   EXISTS ( SELECT 1
                     FROM   ( SELECT    lotid ,
                                        MAX(cleantime) cleantime
                              FROM      tab
                              GROUP BY  lotid
                            ) b
                     WHERE  a.lotid = b.lotid
                            AND a.cleantime = b.cleantime )
      

  4.   

    用row_number函数就可以了哈,非常简洁:select * 
    from
    (
    select *,
           row_number() over(partition by LOTID order by CleanTime desc) as rownum
    from tb
    ) t 
    where rownum=1
      

  5.   

    以上tb也是通过select 过来的表,并不是在数据库中存在的实际表,不知道有没有问题,我再测试下,谢谢了