我尽量表达的详细一点
表的样式是这样的
 表A
到货时间        物料      供应商
2012-1-1      大米      甲
2012-1-2      大米      甲
2012-1-1      小米      已
2012-1-1      小米      甲
....         ....      ....
我想取 所以供应商 所有物料 的第一批的到货时间 把它放到临时表 #b 中
例如 上面的 供应商甲 大米 有两批大米的到货时间 我只想要最早到的那批
到货时间        物料      供应商
2012-1-1      大米      甲
2012-1-1      小米      已
2012-1-1      小米      甲
....         ....      ....
 怎样写sql
谢谢了    

解决方案 »

  1.   


    insert #b
    select * from tb a
    where 到货时间=(select min(到货时间) from tb b 
    where a.物料=b.物料  and a.供应商=b.供应商)
      

  2.   

    select * into #b
    from tb t
    where not exists(select 1 from tb where 物料=t.物料 and 供应商=t.供应商 and 到货时间<t.到货时间)
      

  3.   

    select * from tb t where not exsits(select 1 from tb where 供应商=t.供应商 and 到货时间<t.到货时间)
      

  4.   


    select a.供应商,a.物料,b.到货时间
    from tb a outer apply(
          select top 1 到货时间 from tb where 供应商 = a.供应商 and 物料 = a.物料 order by 到货时间
        ) b