select prodno=a.prodno,qty_std=(a.qty*a.qty_rate-b.qty*b.qty_rate) from a,b where a.prodno=b.prodno
其实不用管那个条件,没意义,两表关联以后sum一下就是了
我覺得還是要sum()一下好點吧。
select prodno, sum(qty) from ( select prodno,qty from 进货表 union all select prodno,qty*(-1) from 出货表 ) as 表 group by prodno
select isnull(i.prodno,o.prodno) prodno, sum(isnull(i.qty,0)-isnull(o.qty,0)) qty from 进货表 i full join 出货表 o on i.prodno=o.prodno group by isnull(i.prodno,o.prodno)
不好意思,搞错了,应该是“进货的时间要小于等于出货的时间” 现在我解决了,是这样的,我忘记了isnull,所以总是结果不对,才到这里浪费大家的时间。 我的代码如下: select a.*,(a.in_qty-out_qty)as std_qty from ( select prodno, sum(qty*qty_rate) as in_qty, (select isnull(sum(qty*qty_rate),0) from 出货表 where prodno=I.prodno) as out_qty from 进货表I group by prodno )a order by prodno
from a,b where a.prodno=b.prodno
(
select prodno,qty from 进货表
union all
select prodno,qty*(-1) from 出货表
) as 表
group by prodno
sum(isnull(i.qty,0)-isnull(o.qty,0)) qty
from 进货表 i full join 出货表 o on i.prodno=o.prodno
group by isnull(i.prodno,o.prodno)
现在我解决了,是这样的,我忘记了isnull,所以总是结果不对,才到这里浪费大家的时间。
我的代码如下:
select a.*,(a.in_qty-out_qty)as std_qty from (
select prodno,
sum(qty*qty_rate) as in_qty,
(select isnull(sum(qty*qty_rate),0) from 出货表
where prodno=I.prodno) as out_qty
from 进货表I
group by prodno
)a
order by prodno