我想进行多表联查,根据相同的存货,查找到相对应的入库和出库明细,查出来的数据有些出入,不知道什么问题?

以下是我的查询代码,但是查出来的数据,发现入库信息和出库信息并未同行显示:

SELECT MAX
    ( cinvcode ) '存货编码',(cbatch)'批号',
    (case  when type='入库' then cdefine30 else null end)'入库台次',
    (case  when type='出库' then cdefine30 else null end)'出库台次',
    (case  when type='入库' then dnmaketime else null end)'入库时间',
    (case  when type='出库' then dnmaketime else null end)'出库时间',
    (case  when type='入库' then ccode else null end)'入库单号',
    (case  when type='出库' then ccode else null end)'出库单号',
    SUM ( CASE WHEN type = '入库' THEN iquantity ELSE 0 END ) '入库量',
    SUM ( CASE WHEN type = '出库' THEN iquantity ELSE 0 END ) '出库量'
FROM
    ( SELECT rdrecords01.cinvcode,rdrecord01.ccode,rdrecords01.cbatch,rdrecord01.dnmaketime,rdrecord01.cmaker,rdrecords01.cdefine30,type = '入库', iquantity FROM rdrecords01,rdrecord01 where rdrecords01.id=rdrecord01.id
    UNION ALL 
    SELECT rdrecords11.cinvcode,rdrecord11.ccode,rdrecords11.cbatch,rdrecord11.dnmaketime,rdrecord11.cmaker,rdrecords11.cdefine30, type = '出库', iquantity FROM rdrecords11,rdrecord11 where rdrecords11.id=rdrecord11.id) t
    GROUP BY
    cinvcode,cbatch,cdefine30,dnmaketime,ccode,type

查询结果:

img

问题说明:
以上数据可以发现,有入库数据,就没有出库数据,有出库数据就没有入库数据,并未同时显示在一列.
目的:
我的目的是要根据相同的存货,查询出对应的入库明细和出库明细,例如:我相查询存货A,何时入库的,入库的数量多少,批号多少,台次多少,何时出库的,出库数量多少,想筛查出入库且有台次的存货,而出库没有台次,想把出库没有台次的这部分数据找到

没太看懂,数据源是什么样,结果想要什么样