现有两个表,表A中是商品的进货明细, 包含商品编码, 进货日期, 进货数量,进货单价; 表B中是商品的销售明细,包含商品编码, 销售日期, 销售数量;
表A: 商品编码 进货日期 进货数量 进货单价
1001 2006-10-01 10 15
1001 2006-10-02 15 13
1002 2006-10-03 10 20
1002 2006-10-02 20 25
表B: 商品编码 销售日期 销售数量
1001 2006-10-05 8
1001 2006-10-06 7
1002 2006-10-04 10
现在要求: 计算出某一个时间段内的每一种商品的销售成本金额.
销售成本金额=商品的平均进货单价乘以销售数量.
表A: 商品编码 进货日期 进货数量 进货单价
1001 2006-10-01 10 15
1001 2006-10-02 15 13
1002 2006-10-03 10 20
1002 2006-10-02 20 25
表B: 商品编码 销售日期 销售数量
1001 2006-10-05 8
1001 2006-10-06 7
1002 2006-10-04 10
现在要求: 计算出某一个时间段内的每一种商品的销售成本金额.
销售成本金额=商品的平均进货单价乘以销售数量.
declare @end datetimeset @start='2006-10-01'
set @end='2006-10-15'select sum(isnull(a.进货单价,0))*sum(isnull(b.销售数量,0))/count(a.商品编码)from 表A a full join 表B bon a.商品编码=b.商品编码 and a.进货日期=b.进货日期where a.进货日期 between @start and @endand b.销售日期 between @start and @endgroup by 商品编码
declare @start datetime
declare @end datetimeset @start='2006-10-01'
set @end='2006-10-15'select sum(isnull(a.进货单价,0))*sum(isnull(b.销售数量,0))/count(a.商品编码) as 销售成本金额from 表A a full join 表B bon a.商品编码=b.商品编码 and a.进货日期=b.进货日期where a.进货日期 between @start and @endand b.销售日期 between @start and @endgroup by 商品编码
from (
select 商品编码,sum(销售数量) as 销售数量
from b
where 销售日期 between '2006-10-1' and '2006-11-1'
group by 商品编码
) as t1 left join (
select 商品编码,sum(进货数量) as 进货数量,sum(进货单价*进货数量) as 金额
from a
where 进货日期 between '2006-10-1' and '2006-11-1'
group by 商品编码
) as t2
on t1.商品编码=t2.商品编码
1001 2006-10-01 10 15
1001 2006-10-02 15 13
1002 2006-10-03 10 20
1002 2006-10-02 20 25
表B: 商品编码 销售日期 销售数量
1001 2006-10-05 8
1001 2006-10-06 7
1002 2006-10-04 10
现在要求: 计算出某一个时间段内的每一种商品的销售成本金额.
销售成本金额=商品的平均进货单价乘以销售数量.
RE:
select B.商品编号,sum(B.销售数量)*C.avgPrice from B
inner join
(
select 商品编号,avg(进货单价/进货数量) avgPrice from A
) C
on B.商品编号=C.商品编号
group by B.商品编号
insert @a
select 1001, '2006-10-01', 10, 15 union all
select 1001, '2006-10-02', 15, 13 union all
select 1002, '2006-10-03', 10, 20 union all
select 1002, '2006-10-02', 20, 25declare @b table(商品编码 varchar(4), 销售日期 datetime, 销售数量 int)
insert @b
select 1001, '2006-10-05' , 8 union all
select 1001, '2006-10-06' , 7 union all
select 1002, '2006-10-04' , 10
/*
现在要求: 计算出某一个时间段内的每一种商品的销售成本金额.
销售成本金额=商品的平均进货单价乘以销售数量.
*/
select 商品编码,[销售成本金额]=销售数量*
(select sum(进货数量*进货单价)/sum(进货数量)
from @a a where 商品编码=b.商品编码)
from @b b(所影响的行数为 4 行)
(所影响的行数为 3 行)商品编码 销售成本金额
---- ----------------------------------------
1001 110.400000
1001 96.600000
1002 233.333330(所影响的行数为 3 行)
insert @a
select 1001, '2006-10-01', 10, 15 union all
select 1001, '2006-10-02', 15, 13 union all
select 1002, '2006-10-03', 10, 20 union all
select 1002, '2006-10-02', 20, 25declare @b table(商品编码 varchar(4), 销售日期 datetime, 销售数量 int)
insert @b
select 1001, '2006-10-05' , 8 union all
select 1001, '2006-10-06' , 7 union all
select 1002, '2006-10-04' , 10
/*
现在要求: 计算出某一个时间段内的每一种商品的销售成本金额.
销售成本金额=商品的平均进货单价乘以销售数量.
*/
select 时间=convert(varchar(10),销售日期,120),商品编码,[销售成本金额]=sum(销售数量)*
(select sum(进货数量*进货单价)/sum(进货数量)
from @a a where 商品编码=b.商品编码)
from @b b where 销售日期 between '2006-10-04 00:00:00' and '2006-10-05 23:59:00'
group by convert(varchar(10),销售日期,120),商品编码(所影响的行数为 4 行)
(所影响的行数为 3 行)时间 商品编码 销售成本金额
---------- ---- ----------------------------------------
2006-10-05 1001 110.400000
2006-10-04 1002 233.333330(所影响的行数为 2 行)
select 商品编码,[销售成本金额]=sum(销售数量)*
(select sum(进货数量*进货单价)/sum(进货数量)
from @a a where 商品编码=b.商品编码)
from @b b where 销售日期 between '2006-10-04 00:00:00' and '2006-10-06 23:59:00'--时间段
group by 商品编码(所影响的行数为 4 行)
(所影响的行数为 3 行)商品编码 销售成本金额
---- ----------------------------------------
1001 207.000000
1002 233.333330(所影响的行数为 2 行)