昨天好不容易把出库数量计算对了,今天又来一个需求要把来源单号加到一起。。。
事情是这样的,我要把一张表里的数据按照规格进行分组,并且按照分组后的结果计算出总数量,这是原sql
select
od.mspec,
rm.mcode,
od.product_type,
od.main_code,
od.length,
sum(od.length * od.total_num) as total_num,
od.detail_no
from table1 od
left join table2 rm on rm.spec = od.mspec
where od.id in
<foreach collection="idSet" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
group by mspec
如果存在多条明细的话 od.detail_no 就会被覆盖掉,我当时就在想,wc,难道要把group by去掉,然后在双层for循环去判断计算,那岂不是寄了啊,要多写N多代码和牺牲N多效率了,然后我苦思冥想,突然灵光乍现,在分组之后貌似用group_concat可以帮我获取到分组后不同的拼接数据,赶紧试试
select
od.mspec,
rm.mcode,
od.product_type,
od.main_code,
od.length,
sum(od.length * od.total_num) as total_num,
group_concat(od.detail_no) as detail_no
from table1 od
left join table2 rm on rm.spec = od.mspec
where od.id in
<foreach collection="idSet" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
group by mspec
试了一下wc还真行,可以的,这个函数省了我不少功夫,本来昨天晚上都在思考总出库数量怎么算的来着
// List<XaleOrderDetail> detailList = new ArrayList<>();
// //根据规格分组
// for (SaleOrderDetail detail : details) {
// if (detailList.size() > 0) {
// for (XdSaleOrderDetail orderDetail : detailList) {
// //如果规格相同
// if (orderDetail.getMspec().equals(detail.getMspec())){
//
// }
// }
// } else {
// detailList.add(detail);
// }
// }
TNND还好昨天脑子还没费,赶紧注释掉了,最后结果如下:
完美!