表A:
表A中的planNumber是唯一的,表B的planNumber不是唯一,与表A相关联;
我现在想通过他们的planNumber关联关系,使表B里面相同的planNumber编号的qty相加,然后与表A的totalQty进行对比;
先对表B进行查询,根据qty进行分组求和。
然后关联查询,判断大小。
select a.planNumber,a.totalqty,c.qty,
case when c.qty=0 then '未下单'
when qty<totalqty then '部分下单'
when qty=totalqty then '全部下单'
else '未知' end
from a
left join
(select b.planNumber,sum(b.qty) qty from sale_order b where b.planNumber <> '' GROUP BY b.planNumber) c
on a.planNumber=c.planNumber
select A.planNumber,(case when C.totalQyt is null or C.totalQyt=0 then '未下单' when C.totalQyt = A.totalQyt then '全部下单' else '部分下单' end) from A left join
(select planNumber,sum(qyt) as totalQyt from B group by planNumber)C on A.planNumber = C.planNumber
SELECT a.planNumber planNumber,a.totalQty totalQty, b.qtys qtys
from 表 A a
LEFT JOIN (SELECT planNumber, sum(qty) qtys FROM 表B GROUP BY planNumber) b on a.planNumber = b.planNumber
having 1=1
--- 如果是postgres 数据库 将别名修改为字段名 比如 将qtys -> b.qtys totalQty -> a.totalQty
<if test="未下单">
and qtys ==0
</if>
<if test="部分下单">
and qtys < totalQty
</if>
<if test="全部下单">
and qtys == totalQty
</if>