100求助:分类汇总的问题,头都大了,请求帮助急!急!急!!!

ROMA_2008 2006-11-20 02:22:04
把销售表和结存表汇总成一个表

销售表:
日期 单据号码 客户名称 送货 开票
2006-1-2 送630 亨通 6
2006-1-12 送631 亨通 4
2006-1-16 送639 金田电工 6
2006-1-20 开237 云通电子 2
2006-1-21 开238 金田电工 4
2006-1-24 送642 长城高新 10

结存表:
客户名称 月份 结存
亨通 2005-12 4
云通电子 2005-12 3
金田电工 2005-12 8
长城高新 2005-12 1

现在要得到每个客户的销售汇总表(本期结存=上期结存+送货-开票)
日期 单据号码 客户名称 送货 开票 结存
上期结存 亨通 4
2006-1-2 送630 亨通 6
2006-1-12 送631 亨通 4
本期结存 亨通 6
上期结存 金田电工 8
2006-1-16 送639 金田电工 6
2006-1-21 开238 金田电工 4
本期结存 金田电工 10
上期结存 云通电子 3
2006-1-20 开237 云通电子 2
上期结存 云通电子 1
上期结存 云通电子 1
2006-1-24 送642 长城高新 10
本期结存 云通电子 11


...全文
227 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
playwarcraft 2006-11-20
  • 打赏
  • 举报
回复
create table sale(rq datetime,num varchar(20),customer varchar(30),send int,code int)
insert into sale
select '2006-01-02','送630','亨通',6,null union all
select '2006-01-12','送631','亨通',4,null union all
select '2006-01-16','送639','金田電子',6,null union all
select '2006-01-20','開237','雲通電子',null,2 union all
select '2006-01-21','開238','金田電子',null,4 union all
select '2006-01-24','送642','長城高新',10,null

create table locate(customer varchar(30),monthly varchar(07),value int)
insert into locate
select '亨通','2005-12',4 union all
select '雲通電子','2005-12',3 union all
select '金田電子','2005-12',8 union all
select '長城高新','2005-12',1

select * from
(select *,null as [結存] from sale
union all
select null,'上期結存',customer,null,null,value from locate
union all
select null,'本期結存',customer,null,null,sum(isnull(結存,0))+sum(isnull(send,0))-sum(isnull(code,0))
from
(select *,null as [結存] from sale
union all
select null,'上期結存',customer,null,null,value from locate) A
group by customer) T
order by customer,case when num='上期結存' then 1
when num='本期結存' then 3
else 2 end, rq

drop table sale,locate

上期結存 亨通 4
2006-01-02 00:00:00.000 送630 亨通 6
2006-01-12 00:00:00.000 送631 亨通 4
本期結存 亨通 14
上期結存 金田電子 8
2006-01-16 00:00:00.000 送639 金田電子 6
2006-01-21 00:00:00.000 開238 金田電子 4
本期結存 金田電子 10
上期結存 長城高新 1
2006-01-24 00:00:00.000 送642 長城高新 10
本期結存 長城高新 11
上期結存 雲通電子 3
2006-01-20 00:00:00.000 開237 雲通電子 2
本期結存 雲通電子 1
rea1gz 2006-11-20
  • 打赏
  • 举报
回复
seelct
-->
select
rea1gz 2006-11-20
  • 打赏
  • 举报
回复
declare @date datetime --参数
set @date='2006-1-1' --查一月

seelct 日期,单据号码,客户名称,送货,开票,结存
from (
select cast(null as datetime) as 日期,'上期结存' as 单据号码,客户名称,cast(null as int) as 送货,cast(null as int) as 开票,结存,
1 as type
from 结存表
where 月份=convert(varchar(7),dateadd(month,-1,@date)
union all
select 日期,单据号码,客户名称,送货,开票,cast(null as int) as 结存,
2 as type
from 销售表
where 日期>=@date and 日期<dateadd(month,1,@date)
union all
select null as 日期,'本期结存' as 单据号码,客户名称,null as 送货,null as 开票,sum(结存) as 结存,
3 as type
from (
select 客户名称,结存
from 结存表
where 月份=convert(varchar(7),dateadd(month,-1,@date)
union all
select 客户名称,isnull(送货,0)-isnull(开票,0) as 结存,
from 销售表
where 日期>=@date and 日期<dateadd(month,1,@date)
) as t
group by 客户名称
) as t1
order by 客户名称,type,日期
dulei115 2006-11-20
  • 打赏
  • 举报
回复
select 日期, 单据号码, 客户名称, 送货, 开票, 结存
from
(select 日期, 单据号码, 客户名称, 送货, 开票, cast(null as int) as 结存, 1 as 排序
from 销售表
union
select null, '上期结存', 客户, null, null, 结存, 0
from 结存表
union
select null, '本期结存', a.客户, null, null, a.结存 + sum(isnull(b.送货, 0)) - sum(isnull(a.开票, 0)), 2
from 结存表 a left join 销售表 b on a.客户 = b.客户名称
group by a.客户, a.结存) aa
order by 客户名称, 排序, 日期
ROMA_2008 2006-11-20
  • 打赏
  • 举报
回复
一定是上一期的
ROMA_2008 2006-11-20
  • 打赏
  • 举报
回复
其他月份类似,只要一个月统计出来,其他月份就好说了
playwarcraft 2006-11-20
  • 打赏
  • 举报
回复
時間概念不清,你的销售表是否只有1個月的資料?结存表是否一定是上期結存資料??
否則所謂的"期"的時間概念如何劃分??

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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