SELECT a.productname, a.containername, a.specname, a.moveinqty, a.moveoutqty
  FROM aaa a, bbb b
 WHERE a.moveouttimestamp IS NOT NULL
   AND a.specname IN
             ('6010', '7010', '7020', '7030', '7040', '7050', '7060', '7070')
   AND a.containername IN (SELECT containername
                             FROM aaa)
   AND a.productname = b.productname
   AND b.cdoname = 'TrackOutLot'
   AND b.txndate =
          (SELECT MAX (b.txndate)
             FROM aaa a, bbb b
            WHERE a.moveouttimestamp IS NOT NULL
              AND a.specname IN
                     ('6010',
                      '7010',
                      '7020',
                      '7030',
                      '7040',
                      '7050',
                      '7060',
                      '7070'
                     )
              AND a.containername = b.containername
              AND a.productname = b.productname
              AND b.cdoname = 'TrackOutLot')这是小弟现在的sql,现在需要的结果是,moveinqty只需要specname是6010的,而moveoutqty则需要列出来的所有specname的相应记录。请问有什么好办法吗?

解决方案 »

  1.   

    SELECT a.productname, a.containername, a.specname, a.moveinqty, b.moveoutqty
      FROM aaa a, bbb b, aaa c
     WHERE a.moveouttimestamp IS NOT NULL
       AND a.specname IN ('6010')
       AND a.containername IN (SELECT containername
                                 FROM aaa)
       AND a.productname = b.productname
       AND b.cdoname = 'TrackOutLot'
       AND b.txndate =
              (SELECT MAX (b.txndate)
                 FROM aaa a, bbb b
                WHERE a.moveouttimestamp IS NOT NULL
                  AND a.specname IN ('6010')
                  AND a.containername = b.containername
                  AND a.productname = b.productname
                  AND b.cdoname = 'TrackOutLot')
       and c.moveouttimestamp IS NOT NULL
       AND c.specname IN
                 ('6010', '7010', '7020', '7030', '7040', '7050', '7060', '7070')
       AND c.containername IN (SELECT containername
                                 FROM aaa)
       AND c.productname = b.productname
       AND b.cdoname = 'TrackOutLot'
       AND b.txndate =
              (SELECT MAX (b.txndate)
                 FROM aaa a, bbb b
                WHERE a.moveouttimestamp IS NOT NULL
                  AND a.specname IN
                         ('6010',
                          '7010',
                          '7020',
                          '7030',
                          '7040',
                          '7050',
                          '7060',
                          '7070'
                         )
                  AND c.containername = b.containername
                  AND c.productname = b.productname
                  AND b.cdoname = 'TrackOutLot')
      

  2.   

    不好意思,Eric_1999(╙@^@╜) ,你的解不对,moveoutqty错了,我的同事用了union all查询:select a.PRODUCTNAME, a.CONTAINERNAME, a.SPECNAME, a.MOVEINQTY, a.MOVEOUTQTY  from aaa a, bbb b where a.MOVEOUTTIMESTAMP is not null   and a.SPECNAME in ='6010'   and a.CONTAINERNAME in (select containername from aaa)   and a.PRODUCTNAME = b.PRODUCTNAME   and b.CDONAME = 'TrackOutLot'   and b.TXNDATE = (select Max(b.TXNDATE) from aaa a, bbb b where a.MOVEOUTTIMESTAMP is not null and a.SPECNAME ='6010' and a.CONTAINERNAME = b.CONTAINERNAME and a.PRODUCTNAME = b.PRODUCTNAME and b.CDONAME = 'TrackOutLot')union allselect a.PRODUCTNAME, a.CONTAINERNAME, a.SPECNAME, '', a.MOVEOUTQTY  from aaa a, bbb b where a.MOVEOUTTIMESTAMP is not null   and a.SPECNAME in ('7010','7030','7050','7070')   and a.CONTAINERNAME in (select containername from aaa)   and a.PRODUCTNAME = b.PRODUCTNAME   and b.CDONAME = 'TrackOutLot'   and b.TXNDATE = (select Max(b.TXNDATE) from aaa a, bbb b where a.MOVEOUTTIMESTAMP is not null and a.SPECNAME in ('7010','7030','7050','7070') and a.CONTAINERNAME = b.CONTAINERNAME and a.PRODUCTNAME = b.PRODUCTNAME and b.CDONAME = 'TrackOutLot')他出错的地方是一开始的a.moveinqty,错误信息:ORA-01790: expression must have same datatype as corresponding expression。
      

  3.   

    现在已经搞定了,但是还有一个问题,我想多加一列,只查询7070的moveoutqty,这样的SQL应该怎么样修改?谢谢!