第一条SQL:
select dtl.mtlid as '物料id',sum(obtainQty) as '用料数',hdr.number as '备料号' from wip_mtl_obtain_dtl dtl left join wip_mtl_obtain_hdr hdr on hdr.id=dtl.obtainid where hdr.obtainkind in(1,2) and hdr.omordernumber='ZS0212-05-08' group by hdr.number,dtl.mtlid
select dtl.mtlid as '物料id',sum(obtainQty) as '备料数',hdr.number as '备料号' from wip_mtl_obtain_dtl dtl left join wip_mtl_obtain_hdr hdr on hdr.id=dtl.obtainid where hdr.obtainkind in(1) and hdr.omordernumber='ZS0212-05-08' group by hdr.number,dtl.mtlid
结果:
上面的两句sql区别就在:
where hdr.obtainkind in(1,2) and hdr.omordernumber='ZS0212-05-08'与
where hdr.obtainkind in(1) and hdr.omordernumber='ZS0212-05-08'
如何能将这两条数据写到一起条sql中了? 分组是按第一组,数据将备料和用料 整合到一起 例子如下(数据错误仅仅是个格式):
这个里面肯定用了 case when,这是一个case when 语句中加入分组聚合的 问题,研究中。 望各位请大侠赐招。 并非小弟吝啬,分就这么多了。
再就是把这两个表作为子表,a1和a2,
select a1.'物料id', a1.'用料数', a1.'备料号, a2.'备料数' from (XXX)a1,(XXX)a2 where a1.'物料id' = a2.'物料id'
嘎嘎,还有左连接右连接,没有环境,不好测试正确性。就不献丑了。
教你个老土的招数,unit all