先求物品的平均单价再求销售总值

chengyin 2006-10-21 03:10:06
现有两个表,表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
现在要求: 计算出某一个时间段内的每一种商品的销售成本金额.
销售成本金额=商品的平均进货单价乘以销售数量.


...全文
214 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
中国风 2006-10-22
  • 打赏
  • 举报
回复
declare @a table(商品编码 varchar(4),进货日期 datetime, 进货数量 int, 进货单价 decimal(15,2))
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, 25

declare @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 行)
中国风 2006-10-22
  • 打赏
  • 举报
回复
declare @a table(商品编码 varchar(4),进货日期 datetime, 进货数量 int, 进货单价 decimal(15,2))
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, 25

declare @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 行)

i9988 2006-10-21
  • 打赏
  • 举报
回复
奇怪 单价/数量


Well 2006-10-21
  • 打赏
  • 举报
回复
表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
现在要求: 计算出某一个时间段内的每一种商品的销售成本金额.
销售成本金额=商品的平均进货单价乘以销售数量.
RE:
select B.商品编号,sum(B.销售数量)*C.avgPrice from B
inner join
(
select 商品编号,avg(进货单价/进货数量) avgPrice from A
) C
on B.商品编号=C.商品编号
group by B.商品编号
i9988 2006-10-21
  • 打赏
  • 举报
回复
select t1.*,t1.销售数量*t2.金额/t2.进货数量 as 销售成本金额
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.商品编码

mschen 2006-10-21
  • 打赏
  • 举报
回复

declare @start datetime
declare @end datetime

set @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 b

on a.商品编码=b.商品编码 and a.进货日期=b.进货日期

where a.进货日期 between @start and @end

and b.销售日期 between @start and @end

group by 商品编码
mschen 2006-10-21
  • 打赏
  • 举报
回复
-- have a try!

declare @start datetime
declare @end datetime

set @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 b

on a.商品编码=b.商品编码 and a.进货日期=b.进货日期

where a.进货日期 between @start and @end

and b.销售日期 between @start and @end

group by 商品编码
chengyin 2006-10-21
  • 打赏
  • 举报
回复
补充一点: 在一个SQL语句中实现.

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧