select a.年,a.月,a.网点名称,a.销售金额,a.成本
,销售毛利=a.销售金额-a.成本
,b.费用,净利润=a.销售金额-a.成本-b.费用
from(
select 年=year(销售日期),月=month(销售日期)
,网点名称,销售金额=sum(销售金额),成本=sum(成本)
from 销售明细表
group by year(销售日期),month(销售日期),网点名称
) a join(
select 年=year(日期),月=month(日期),网点名称,费用=sum(费用)
from 费用明细
group by year(日期),month(日期),网点名称
) b on a.年=b.年 and a.月=b.月 and a.网点名称=b.网点名称

解决方案 »

  1.   

    邹建大哥,我看不懂啊。a和b是什么意思啊,a是不是代表“销售明细表”,b是不是“费用明细”?哪a.年,a.月,又是什么意思啊?
      

  2.   

    a表示select 年=year(销售日期),月=month(销售日期)
    ,网点名称,销售金额=sum(销售金额),成本=sum(成本)
    from 销售明细表
    group by year(销售日期),month(销售日期),网点名称
    生成的子表
    b指select 年=year(日期),月=month(日期),网点名称,费用=sum(费用)
    from 费用明细
    group by year(日期),month(日期),网点名称生成的子表
      

  3.   

    select 年=year(销售日期),月=month(销售日期)
    ,网点名称,销售金额=sum(销售金额),成本=sum(成本)
    from 销售明细表
    group by year(销售日期),month(销售日期),网点名称
    这个结果集的别名a。
    b是select 年=year(日期),月=month(日期),网点名称,费用=sum(费用)
    from 费用明细
    group by year(日期),month(日期),网点名称
    的别名
      

  4.   

    用LEFT JOIN 就沒有問題

    select 年=year(a.销售日期),月=month(a.销售日期),a.网点名称,
           a.网点名称,sum(a.销售金额),sum(a.成本),
           销售毛利=sum(a.销售金额-a.成本),
           sum(b.费用),净利润=sum(a.销售金额-a.成本-b.费用)
    from 销售明细表 a 
    left join 费用明细 b
    on a.网点名称=b.网点名称
    group by year(a.销售日期),month(a.销售日期),a.网点名称
      

  5.   

    谢谢楼上的大哥,这样做是可以的,可是还有一个问题,如果第二张费用表中有某一个网点的记录,而第一张表销售表中没有,如果用right join,可以。那么,如果第一张销售表中有某一个网点的记录,而第二张表销售表中没有,又只能用 left join了,这样是不是不行啊。我设想的是,两种情况同时存在。我要的是下面的结果啊,再一次求教,十分感谢:销售日期   网点名称    销售金额    成本  
    2004-1-03   温州         250        200
    2004-1-05   温州         300        260
    2004-1-07   台州         450        380
    2004-1-08   台州         110        50
    2004-1-30   金华         200        180
    日期         网点名称    费用
    2004-1-7      温州        60
    2004-1-10     温州        10
    2004-1-11     台州        20
    2004-1-12     台州        10
    2004-1-20     宁波        20我现在需要做一个查询得到如结果
      年   月   网点名称   销售金额    成本   销售毛利    费用    净利润
    2004   1      温州      550        460      90        70       20
    2004   1      台州      560        430      130       30       100
    2004   1      宁波      0          0        0         20       -20
    2004   1      金华      200        180      20        0        20
      

  6.   

    select 年=isnull(a.年,b.年)
    ,月=isnull(a.月,b.月)
    ,网点名称=isnull(a.网点名称,b.网点名称)
    ,销售金额=isnull(a.销售金额,0)
    ,成本=isnull(a.成本,0)
    ,销售毛利=isnull(a.销售金额,0)-isnull(a.成本,0)
    ,费用=isnull(b.费用,0)
    ,净利润=isnull(a.销售金额,0)-isnull(a.成本,0)-isnull(b.费用,0)
    from(
    select 年=year(销售日期),月=month(销售日期)
    ,网点名称,销售金额=sum(销售金额),成本=sum(成本)
    from 销售明细表
    group by year(销售日期),month(销售日期),网点名称
    ) a full join(
    select 年=year(日期),月=month(日期),网点名称,费用=sum(费用)
    from 费用明细
    group by year(日期),month(日期),网点名称
    ) b on a.年=b.年 and a.月=b.月 and a.网点名称=b.网点名称
      

  7.   

    --测试数据
    declare @销售明细表 table(销售日期 datetime,网点名称 varchar(10),销售金额 int, 成本 int)
    insert into @销售明细表
    select '2004-1-03','温州',250,200
    union all select '2004-1-05','温州',300,260
    union all select '2004-1-07','台州',450,380
    union all select '2004-1-08','台州',110,50
    union all select '2004-1-30','金华',200,180declare @费用明细 table(日期 datetime,网点名称 varchar(10),费用 int)
    insert into @费用明细
    select '2004-1-7','温州',60
    union all select '2004-1-10','温州',10
    union all select '2004-1-11','台州',20
    union all select '2004-1-12','台州',10
    union all select '2004-1-20','宁波',20--查询
    select 年=isnull(a.年,b.年)
    ,月=isnull(a.月,b.月)
    ,网点名称=isnull(a.网点名称,b.网点名称)
    ,销售金额=isnull(a.销售金额,0)
    ,成本=isnull(a.成本,0)
    ,销售毛利=isnull(a.销售金额,0)-isnull(a.成本,0)
    ,费用=isnull(b.费用,0)
    ,净利润=isnull(a.销售金额,0)-isnull(a.成本,0)-isnull(b.费用,0)
    from(
    select 年=year(销售日期),月=month(销售日期)
    ,网点名称,销售金额=sum(销售金额),成本=sum(成本)
    from @销售明细表
    group by year(销售日期),month(销售日期),网点名称
    ) a full join(
    select 年=year(日期),月=month(日期),网点名称,费用=sum(费用)
    from @费用明细
    group by year(日期),month(日期),网点名称
    ) b on a.年=b.年 and a.月=b.月 and a.网点名称=b.网点名称/*--测试结果
    年    月     网点名称 销售金额  成本    销售毛利  费用    净利润   
    ----- ----- ----------- ---------- ----------- ----------- ---
    2004  1     金华   200   180   20    0     20
    2004  1     宁波   0     0     0     20    -20
    2004  1     台州   560   430   130   30    100
    2004  1     温州   550   460   90    70    20(所影响的行数为 4 行)
    --*/