Try:
select T.cpbh,
sum(T.sl),
sum(T.sl*T.jg),
sum(T.sl)*(select sum(dj) WLDA where wlbh in(select wlbh from PF where cpbh=T.cpbh))*(select sum(yl) from PF where cpbh=T.cpbh),
sum(T.sl*T.jg)-sum(T.sl)*(select sum(dj) WLDA where wlbh in(select wlbh from PF where cpbh=T.cpbh))*(select sum(yl) from PF where cpbh=T.cpbh)
from XFJLB T
group by cpbh

解决方案 »

  1.   

    select
        a.cpbh,
        sum(a.sl*a.jg),
        sum(a.sl*c.ckpjdj*b.yl),
        sum(a.sl*a.jg)-sum(a.sl*c.ckpjdj*b.yl)
    from
        XFJLB a,
        PF    b,
        WLDA  c
    where
        a.cpbh=b.cpbh and b.wlbh=c.wlbh
    group by
        a.cpbh
      

  2.   

    --sum(sl*wlda.ckpjdj*pf.yl),你公式中写错了吧,应该是:sum(sl*wlda.dj*pf.yl)select
        a.cpbh as 编号,
        sum(a.sl) as 销售数量,
        sum(a.sl*a.jg) as 销售金额,
        sum(a.sl*c.dj*b.yl) as 物料成本,
       sum(a.sl*a.jg)-sum(a.sl*c.dj*b.yl) as 利润
    from XFJLB a inner join PF b on a.cpbh=b.cpbh 
        inner join WLDA  c on b.wlbh=c.wlbh
    group by a.cpbh
      

  3.   

    因为xfjlb中不一定是一个菜点一次,还有 pf中是一个菜对应多种料啊