物料编码 所在工位  加工日期
0001       a        20100701
0001       b        20100702
见上表,数据很多,想得到某个编码的最后加工日期的这一行的数据(要包含所在工位)。谢谢!select  物料编码,所在工位,max(加工日期) from table1 group by 物料编码,所在工位,加工日期
这样不行,会把所有的日期都列出来。但是如果select  物料编码,max(加工日期) from table1 group by 物料编码,加工日期
又不能列出所在工位列。谢谢!

解决方案 »

  1.   

    SELECT 物料编码,所在工位,加工日期 FROM TB T WHERE 加工日期=(select max(加工日期) from tb where 物料编码=t.物料编码)也可以连接查询啊,
      

  2.   

    select * from tb t
    where (select coung(*) from tb where t.物料编码=物料编码 and t.加工日期<加工日期)<1
      

  3.   

    [sql server] 分组取最大最小常用sql 
    http://blog.csdn.net/xys_777/archive/2010/07/03/5711128.aspx
      

  4.   


    select * from tb a
    where not exists(select 1 from tb and 物料编码=a.物料编码 and 加工日期 > a.加工日期 )