求个sql语句 access

aelfintiger 2016-09-06 02:46:15
表A(入库)
hth cdate sl
001 2016-9-1 3000
002 2016-9-1 200

表B(出库)
hth cdate sl
001 2016-9-2 1000
002 2016-9-3 20

如何得到如下库存表:
hth cdate sl
001 2016-9-1 3000
001 2016-9-2 2000
002 2016-9-1 200
002 2016-9-3 180
...全文
174 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
rings2000 2016-09-06
  • 打赏
  • 举报
回复
每天的库存,学习了ing。
中国风 2016-09-06
  • 打赏
  • 举报
回复
用以下方法测测

SELECT hth,cdate,(SELECT SUM(sl) FROM A WHERE hth=t.hth AND cdate<=t.cdate)-(SELECT SUM(sl) FROM B WHERE hth=t.hth AND cdate<=t.cdate) sl1 FROM A AS t
UNION ALL
SELECT hth,cdate,(SELECT SUM(sl) FROM A WHERE hth=t.hth AND cdate<=t.cdate)-(SELECT SUM(sl) FROM B WHERE hth=t.hth AND cdate<=t.cdate) sl1 FROM B AS t
ORDER BY hth,cdate
卖水果的net 版主 2016-09-06
  • 打赏
  • 举报
回复
select hth,    cdate,              sl from A
union all
select hth,    cdate,              sl from B
order by 1,2

Neo_whl 2016-09-06
  • 打赏
  • 举报
回复

--随便写了个,可以继续优化:
create table #input
(hth char(3),
 cdate datetime,  
 sl int
)

insert into #input
select '001','2016-9-1',3000 union all
select '002','2016-9-1',200

go
create table #output
(hth char(3),
 cdate datetime,  
 sl int
)
go
insert into #output
select '001','2016-9-2',1000 union all
select '002','2016-9-3',20

select i.* from #input as i join #output as o on i.hth=o.hth union all
select o.hth,o.cdate,i.sl-o.sl from #input as i join #output as o on i.hth=o.hth
order by i.hth,i.cdate

34,590

社区成员

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

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