你的rdrecord表里面应该有cinvcode和iunitcost吧,有的话select a.id,a.cinvcode,a.iunitcost from rdrecord a,(select cinvcode,max(ddate) as ddate from rdrecord group by cinvcode) b where a.cinvcode=b.cinvcode and a.ddate=b.ddate

解决方案 »

  1.   

    select a.cInvCode,a.iUnitCost from rdrecords a ,
    (select id,max(dDate) as dDate from rdrecord group by id)b
    where a.id=b.id
      

  2.   

    wanyingsong(豌豆)就是没有呢,两个表连接只有一个id.
      

  3.   

    LoveSQL(努力奋斗ing)呵呵,这样子我试过啦,查询出来的结果是有很多相同的货物编码,按道理来说,结果里的货物编码应该是唯一的。cInvCode                          dDate         iUnitCost
    (这个应该是唯一,不能出现重复)
      

  4.   

    一般情况下,这样写应该可以的
    select max(a.id) as id,b.cinvcode,b.iunitcost,max(a.ddate) from rdrecord a,rdrecords b where a.id=b.id group by b.cinvcode,b.iunitcost
      

  5.   

    如果不要id,就简单了,呵呵
    select b.cinvcode,b.iunitcost,max(a.ddate) from rdrecord a,rdrecords b where a.id=b.id group by b.cinvcode,b.iunitcost
      

  6.   

    select b.cinvcode,b.iunitcost,max(a.ddate) from rdrecord a,rdrecords b where a.id=b.id group by b.cinvcode,b.iunitcost
    order by cinvcode
    /*
    select b.cinvcode,max(a.ddate) from rdrecord a,rdrecords b where a.id=b.id group by b.cinvcode
    order by cinvcode
    */上面查询出来的结果有7436条,下面查询出来有2995条。为什么会多出来这么多条?
    就是因为查询出来的货物编码还是有重复的,为什么会有重复的呢,那是因为它的采购成本不一样(在不同的日期,可这个日期却并不是只有最大日期)。难题就是在这里了。
    记录应该是下面的结果,可却没有办法显示采购成本。有点晕~》
      

  7.   

    表不可能有问题呀。用友的呢。
    上面表记录是每一次采购的记录嘛,一个ID代表一次采购情况,中间就有采购日期的字段。
    然后下一个表就是每一次采购记录中的详细情况咯。有货物编码及相应的采购成本啦。
    就是或下一个表中的ID可以是重复的,每个ID就是来自上一不表表示每一次采购的记录。采购日期当然可以与采购成本一对多的取啊。
    每个采购日期(某一天)可以采购很多的货物啊。然后这一批货物就是有一个共同的ID咯。现在要查询的就是每一件货物最近那天的采购成本。
      

  8.   

    --日期是datetime型的话,可以实现
    select e.* from rdrecords e,
       --每个货物的最后购进单据
     (select distinct a.id,a.dDate,b.cInvCode from rdrecord a,
      (
       --每个货物的最后购进日期
       select d.cInvCode,max(c.dDate) as dDate from rdrecord c,rdrecords d where c.id=d.id group by d.cInvCode
       )b where a.dDate=b.dDate
     ) f
    where e.id=f.id
      

  9.   

    --日期是datetime型的话,可以实现
    select e.* from rdrecords e,
       --每个货物的最后购进单据
     (select distinct a.id,a.dDate,b.cInvCode from rdrecord a,
      (
       --每个货物的最后购进日期
       select d.cInvCode,max(c.dDate) as dDate from rdrecord c,rdrecords d where c.id=d.id group by d.cInvCode
       )b where a.dDate=b.dDate
     ) f
    where e.id=f.id and e.cInvCode=f.cInvCode--少了一个条件
      

  10.   

    苹果写的很不错呢,可是为什么还有货物代码重复的呢?而且我加个distinct 还有重复。select distinct cInvCode from rdrecords 得2997条记录。(比以前多了两条——合理)那应该查询出来的结果也是如此多的记录,可我根据你给的查询再加个distinct e.cInvCode
    居然是3318条记录?思考中……非常感谢你的回答!
      

  11.   

    呵呵,谢谢,问题已经得到妥善解决。
    谢谢楼上的兄弟,更谢谢
     lxd99423(苹果) 。
    不过这里就不给分给你啦,呵呵。下面新开帖给分,虽然不值钱,不过谢谢你的真诚,呵呵。不知能够能为朋友,我的QQ:6310756 请验证:lxd99423(苹果)http://community.csdn.net/Expert/topic/3332/3332963.xml?temp=.9529993