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.网点名称
,销售毛利=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.网点名称
,网点名称,销售金额=sum(销售金额),成本=sum(成本)
from 销售明细表
group by year(销售日期),month(销售日期),网点名称
生成的子表
b指select 年=year(日期),月=month(日期),网点名称,费用=sum(费用)
from 费用明细
group by year(日期),month(日期),网点名称生成的子表
,网点名称,销售金额=sum(销售金额),成本=sum(成本)
from 销售明细表
group by year(销售日期),month(销售日期),网点名称
这个结果集的别名a。
b是select 年=year(日期),月=month(日期),网点名称,费用=sum(费用)
from 费用明细
group by year(日期),month(日期),网点名称
的别名
如
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.网点名称
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
,月=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.网点名称
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 行)
--*/