select (select top 1 a.po from tab a where a.lot=c.lot order by a.andate) as po,
lot,
(select min(andate) from tab b where b.lot=c.lot) as andate
from tab c
group by LOT

解决方案 »

  1.   

    SELECT IDENTITY(int, 1,1) AS AutoID, PO, LOT, AnDate
    INTO #tempTable
    FROM theTable
    ORDER BY LOT, AnDateSELECT PO, LOT, AnDate
    FROM #tempTable
    INNER JOIN (
    SELECT LOT, MAX(AutoID) AS MaxID
    FROM #tempTable
    GROUP BY LOT
    ) B ON B.LOT = A.LOT
    AND B.MaxID = A.AutoID
      

  2.   

    看看,这个行不行咯。
    select distinct * from a where andate in (select min(andate) from a )
      

  3.   

    declare @a table(PO char(6),LOT char(5),AnDate datetime)
    insert into @a
    select '235253', '33391', '2004-5-2 0:00'  union all
    select '235254', '33391', '2004-5-9 0:00'  union all
    select '235255', '33391', '2004-5-16 0:00' union all
    select '235251', '33391', '2004-3-28 0:00' union all
    select '235251', '33391', '2004-4-18 0:00' union all
    select '235252', '33391', '2004-4-11 0:00' union all
    select '235249', '33391', '2004-3-28 0:00' union all
    select '235250', '33391', '2004-3-28 0:00' union all
    select '235250', '33391', '2004-4-18 0:00' union all
    select '235260', '33392', '2004-5-9 0:00'  union all
    select '235258', '33392', '2004-3-28 0:00' union all
    select '235258', '33392', '2004-4-25 0:00' union all
    select '235259', '33392', '2004-4-11 0:00' union all
    select '235256', '33392', '2004-3-28 0:00' union all
    select '235257', '33392', '2004-3-28 0:00' union all
    select '235257', '33392', '2004-4-25 0:00'--查询一
    select PO=min(PO),a.LOT,a.AnDate from
    (
    select LOT,AnDate=Min(AnDate) from @a 
    group by LOT
    ) x,@a a
    where x.LOT=a.LOT and x.AnDate=a.AnDate
    group by a.LOT,a.AnDate--查询二(如果PO,AnDate不同时为小就不正确)
    select PO=Min(PO),Lot,AnDate=Min(AnDate)
    from @a
    group by LOT
      

  4.   

    谢谢 xiaoliaoyun(流浪的云),但因为我这个表有许多列,用你这种方法可能不行;谢谢 haoK(haoK.Y),用临时表我也知道可以解决,但最好不用临时表。????