共四个表
订单主表order(sno(流水号),no(单号))订单子表orders(sno,no,pno(料号))进货主表in(sno,no,status(表的状态,如验收,进货))进货子表ins(sno,no,pno,qty(数量))现在需要列出订单表中所有料号和每个料号在“验收”状态的数量。请问SQL要怎么写?谢谢!
订单主表order(sno(流水号),no(单号))订单子表orders(sno,no,pno(料号))进货主表in(sno,no,status(表的状态,如验收,进货))进货子表ins(sno,no,pno,qty(数量))现在需要列出订单表中所有料号和每个料号在“验收”状态的数量。请问SQL要怎么写?谢谢!
no是每张单的单号。
order
sno no
1 001orders
sno no pno
1 001 wangwangin
sno no status
1 000001 验收ins
sno no pno qty
1 00001 wangwang 1000查询要得到的结果:
pno qty
wangwang 1000
b.pno,
sum(case c.status when '验收' then 1 else 0 end) as 数量
from
order a,订单子表 b,in c,进货子表 d
where
a.no=b.no
and
c.no=d.no
and
b.pno=d.pno
and
a.no=c.no
group by
b.pno
b.pno,
sum(case c.status when '验收' then 1 else 0 end) as 数量
from
order a,订单子表 b,in c,进货子表 d
where
a.no=b.no
and
c.no=d.no
and
b.pno=d.pno
and
a.sno=c.sno
group by
b.pno