PartNo UnitPrice      CutIn_From Seller Expr1 Buyer Cnt
0001     10            2009-4-23     AAA      M        BBB      22
0001     12            2009-4-25     AAB      M        BBB      24
0002     20            2009-4-27     AAA      M        BBB      26
0002     22            2009-4-29     AAB      M        BBB      28
0002     24            2009-4-30     AAB      M        BBB      29如何找出每个PartNo中Cnt最小的行?结果为:
PartNo UnitPrice      CutIn_From Seller Expr1 Buyer Cnt
0001     10            2009-4-23     AAA      M        BBB      22
0002     20            2009-4-27     AAA      M        BBB      26谁能告诉我,谢谢!

解决方案 »

  1.   

    select *
    from tb t
    where not exists(
        select 1
        from tb 
        where partno=t.partno
          and UnitPrice<t.UnitPrice)
      

  2.   

    select * from tab t where not exists (select 1 from tab where t.PartNo=PartNo and
    Cnt<t.Cnt)
      

  3.   

    select PartNo,min(UnitPrice),CutIn_From,Seller,Expr1,Buyer,Cnt from tb group by 
    PartNo,CutIn_From,Seller,Expr1,Buyer,Cnt
      

  4.   

    select *
    from tb t
    where not exists(
        select 1
        from tb 
        where partno=t.partno
          and cnt<t.cnt)modify.不好意思,看错.
      

  5.   

    select * from tb a where UnitPrice=(select min(UnitPrice) from tb where PartNo=a.PartNo)
      

  6.   

    select * from tb a where Cnt=(select min(Cnt) from tb where PartNo=a.PartNo)
      

  7.   

    ----------------------------------------------------------------
    -- Author :fredrickhu(小F 向高手学习)
    -- Date   :2009-07-06 17:41:12
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    create table [tb]([PartNo] varchar(4),[UnitPrice] int,[CutIn_From] datetime,[Seller] varchar(3),[Expr1] varchar(1),[Buyer] varchar(3),[Cnt] int)
    insert [tb]
    select '0001',10,'2009-4-23','AAA','M','BBB',22 union all
    select '0001',12,'2009-4-25','AAB','M','BBB',24 union all
    select '0002',20,'2009-4-27','AAA','M','BBB',26 union all
    select '0002',22,'2009-4-29','AAB','M','BBB',28 union all
    select '0002',24,'2009-4-30','AAB','M','BBB',29
    --------------开始查询--------------------------
    select * from tb a where Cnt=(select min(Cnt) from tb where PartNo=a.PartNo)
    ----------------结果----------------------------
    /*PartNo UnitPrice   CutIn_From                                             Seller Expr1 Buyer Cnt         
    ------ ----------- ------------------------------------------------------ ------ ----- ----- ----------- 
    0001   10          2009-04-23 00:00:00.000                                AAA    M     BBB   22
    0002   20          2009-04-27 00:00:00.000                                AAA    M     BBB   26(所影响的行数为 2 行)
    */
      

  8.   

    fredrickhu的也不行啊
    只查出来了1条数据
      

  9.   

    --这个只有一条?
    select * from tb a where Cnt=(select min(Cnt) from tb where PartNo=a.PartNo)
      

  10.   

    --这个呢?
    select * from tb t
    where not exists(select 1 from tb where partno=t.partno and cnt<t.cnt)
      

  11.   

    这个只有一条select * from tb a where Cnt=(select min(Cnt) from tb where PartNo=a.PartNo)
    因为min只能查到表里最小的那个值吧这个一条都没有
    select * from tb t
    where not exists(select 1 from tb where partno=t.partno and cnt<t.cnt)
      

  12.   

    SELECT DataTable.PartNo, Min(DataTable.Cnt) AS MinOfCnt
    FROM DataTable
    GROUP BY DataTable.PartNo;这样写的话可以找出我所需要的20条数据了
    但是要怎么把其它字段的值也显示?
      

  13.   

    没办法 再用一个Join 解决
      

  14.   


    declare @tb table([PartNo] varchar(4),[UnitPrice] int,[CutIn_From] datetime,[Seller] varchar(3),[Expr1] varchar(1),[Buyer] varchar(3),[Cnt] int)
    insert @tb
    select '0001',10,'2009-4-23','AAA','M','BBB',22 union all
    select '0001',10,'2009-4-23','AAC','M','BBB',22 union all
    select '0001',12,'2009-4-25','AAB','M','BBB',24 union all
    select '0002',20,'2009-4-27','AAA','M','BBB',26 union all
    select '0002',22,'2009-4-29','AAB','M','BBB',28 union all
    select '0002',24,'2009-4-30','AAB','M','BBB',29select [PartNo],[UnitPrice],[CutIn_From],[Seller],[Expr1],[Buyer],[Cnt]
    from
    (
    select *,ord=(row_number() over (partition by PartNo order by cnt)) from @tb
    ) t
    where ord=1/*
    PartNo  UnitPrice  CutIn_From               Seller  Expr1  Buyer  Cnt
    0001    10         2009-04-23 00:00:00.000  AAA     M      BBB    22
    0002    20         2009-04-27 00:00:00.000  AAA     M      BBB    26
    */