CREATE PROCEDURE prg_cbhs  (@ldt_start datetime,@ldt_end datetime)
AS
select f_billid =a.f_billid,    ----------客户订单号
          f_llbillid =b.f_billid     ------ 生产指今单号
into #table1
from t_sellorder a ,t_task b
where a.f_billid =b.f_planno and a.f_date between @ldt_start and @ldt_end
select f_billid =a.f_billid,
           f_llbillid =a.f_llbillid,
          f_xsje =sum(b.f_xsje),
          f_jhcb =sum(b.f_jhcb),
          f_sjcb =sum(b.f_sjcb)
from
#table1 a,
(
select f_billid =d.f_billid,
          f_xsje =sum(d.f_discountmoney),   -----------订单金额
          f_jhcb =0,
          f_sjcb =0
from t_sellorderdetail d ,#table1 s
where s.f_billid =d.f_billid
group by d.f_billidunion all
select f_planno,
        f_xsje =0,
f_jhcb = sum(f_jhcb),
f_sjcb=0
from(
select m.f_planno,
           
           f_jhcb =(select f_baseprice from t_product where t_product.f_id = d.f_goodsno),   --------------生产指令单金额
           f_sjcb =0
from t_task m, t_taskdetail d ,#table1 s
     -- (select f_baseprice from t_product where t_product.f_id = d.f_goodsno) p
where m.f_billid =d.f_billid and s.f_llbillid =m.f_billid
)a group by f_plannounion all         ------------领料单--退料
select f_billid =f_linkbill,
          f_xsje =0,
           f_jhcb =0,
f_sjcb =sum(f_sjcb)
from(
select d.f_linkbill,
           f_sjcb =(select f_baseprice from t_product where t_product.f_id = d.f_goodsno)
from t_drawgoods m ,t_drawgoodsdetail d,#table1 s
where m.f_billid =d.f_billid  and s.f_llbillid =d.f_linkbill 
)a group by f_linkbillunion all --------------补料单
select f_billid =f_linkbill,
           f_xsje =0,
           f_jhcb =0,
f_sjcb=sum(f_sjcb)
from(
select d.f_linkbill,
           f_sjcb =(select f_baseprice from t_product where t_product.f_id = d.f_goodsno)
from t_addgoods m ,t_addgoodsdetail d,#table1 s
where m.f_billid =d.f_billid  and s.f_llbillid =d.f_linkbill 
)a group by f_linkbill
          ) bgroup by a.f_billid,a.f_llbillid
GO

解决方案 »

  1.   

    邹老板不好意思写错了。f_price 是单价,写在另一个基本资料本里聚合函数里不可以有查询,怎么根据 数量 和 单价 算出 金额 再 合计金额?
      

  2.   

    是那个表,但我这样好像错了,现在越来越乱了。你看下:select m.f_planno,
         f_xsje =0,
         f_price,
         f_jhcb =sum(d.f_qty * p.f_baseprice ),   --------------生产指令单金额
         f_sjcb =0
    from t_task m, t_taskdetail d ,#table1 s,
        (select f_baseprice,f_id from t_product a, t_taskdetail d where a.f_id =d.f_goodsno) p
    where m.f_billid =d.f_billid and s.f_llbillid =m.f_billid 
    group by m.f_planno