数据库有以下字段:
入库表:(vdate入库日期,nb为物料编号,name为物料名称,amount为入库数量)
vdate nb name amount
2005-04-01 001 原珠笔 1000
2005-05-01 002 水性笔 3000
2005-05-01 001 原珠笔 2000出库表:(odate出库日期,nb为物料编号,name为物料名称,amount出库数量)
odate nb name amount
2005-04-01 001 原珠笔 100
2005-05-01 002 水性笔 100
2005-05-02 001 原珠笔 200现在想求出2005年5月1日至2005年5月30日的库存表:
(综合性很强的罗!)详细如下:
注:
上次库存是(2005年5月1日以前的入库数量)-(2005年5月1日以前的出库数量)
本月入库是(2005年5月1日至2005年5月30日的入库数量)
本月出库是(2005年5月1日至2005年5月30日的出库数量)
还剩库存是(上次库存+本月入库-本月出库)物料编号 物料名称 上次库存 本月入库 本月出库 还剩库存
001 原珠笔 900 2000 200 2700
002 水性笔 0 3000 100 2900
帮忙一下,谢谢
入库表:(vdate入库日期,nb为物料编号,name为物料名称,amount为入库数量)
vdate nb name amount
2005-04-01 001 原珠笔 1000
2005-05-01 002 水性笔 3000
2005-05-01 001 原珠笔 2000出库表:(odate出库日期,nb为物料编号,name为物料名称,amount出库数量)
odate nb name amount
2005-04-01 001 原珠笔 100
2005-05-01 002 水性笔 100
2005-05-02 001 原珠笔 200现在想求出2005年5月1日至2005年5月30日的库存表:
(综合性很强的罗!)详细如下:
注:
上次库存是(2005年5月1日以前的入库数量)-(2005年5月1日以前的出库数量)
本月入库是(2005年5月1日至2005年5月30日的入库数量)
本月出库是(2005年5月1日至2005年5月30日的出库数量)
还剩库存是(上次库存+本月入库-本月出库)物料编号 物料名称 上次库存 本月入库 本月出库 还剩库存
001 原珠笔 900 2000 200 2700
002 水性笔 0 3000 100 2900
帮忙一下,谢谢
解决方案 »
- 求助vb ListView控件的Tag 属性怎么用呢
- VB中printer打印图形怎样设置横向打印
- 实时错误 '-2147217900(80040e14)怎么解决?
- 请问有哪一位知道"哥德巴赫猜想"如何解决,请写出代码!谢谢!!!
- 急,我用TextStream对象打开一个文本文件一行一行的读出数据,可用SkipLine方法,怎么不是一行一行的读呢?
- VSFLEXGRID與一記錄集綁定,如何讓使用者自己定義列的順序?
- 我在程序中调用一个Vb的可执行程序 是否可以向可执行程序中传递参数呢?
- 关于javascript的问题?(急急)
- 简单问题2
- vb的错误处理程序请教?
- 无边框窗体的问题
- 在应用程序中给自己开发的控件添加选中句柄,好似VB的编辑界面时,窗体中的控件效果,有8句柄能移动,这要在我程序运行时实现
那位,来一句。。谢谢!
我岂不是还要在sql server里面create table...
insert into table...
这个很烦的阿
我就是对JOIN不熟啊。。
期待中!
insert into
贴出来么
SQL SERVER2000的
create table tmp_output(vdate datetime,nd varchar(20),name varchar(20),amount int )insert into tmp_input
select '2005-04-01','001','原珠笔',1000
union
select '2005-05-01','002','水性笔', 3000
union
select '2005-05-01','001','原珠笔', 2000insert into tmp_output
select '2005-04-01','001','原珠笔',100
union
select '2005-05-01','002','水性笔',100
union
select '2005-05-02','001','原珠笔',200--某一个时间点的库存
select nd,name,sum(case type when 'in' then amount else 0 end)-sum(case type when 'out' then amount else 0 end)
from
(
select nd,name,vdate,amount,'in' as Type from tmp_input
union
select nd,name,vdate,amount,'out' as type from tmp_output
)
a where vdate<='2005-05-30'
group by nd,name
就是一个产品本月入库表中有,出库表中没有,或者上个月有这个月没有,等等类似这样的情况
所以要象 icedut(冰) 所说的,要有一个产品表来解决这样的问题
select nd,name,vdate,amount,'in' as Type from tmp_input
'in' as Type 是什么东东啊?
type 表示类型列as type 相当于生成一个列名为type的列
等下,我两张表的结构不一样,这样用UNION好像不行吧?
等下,我两张表的结构不一样,这样用UNION好像不行吧?--
我取得字段结构是一样的不就行了么
select nd,name,sum(case type when 'in' then amount else 0 end)-sum(case type when 'out' then amount else 0 end)
from
(
select nd,name,vdate,amount,'in' as Type from tmp_input
union
select nd,name,vdate,amount,'out' as type from tmp_output
)
a where vdate<='2005-05-30'
group by nd,name不行啊,这只能仅仅是库存
能不能一步到位就有:
物料编号 物料名称 上次库存 本月入库 本月出库 还剩库存谢谢你。。
....
where a.物料编号 =b.物料编号 and a.物料编号 =c.物料编号
....
where a.物料编号 =b.物料编号 and a.物料编号 =c.物料编号
from tmp_input
where vdate>='2005-05-01' and vdate<='2005-05-31' --本月入
group by nd,nameselect nd,name,sum(amount) as OutQty
from tmp_output
where vdate>='2005-05-01' and vdate<='2005-05-31' --本月出
group by nd,name
不行啊,我觉得如果能用SQL语句一步取出来,这是最好的。
不然太多事了。
select d.nd,d.name,d.Stock,e.inQty,f.outqty
from (
select nd,name,
sum(case type when 'in' then amount else 0 end)-sum(case type when 'out' then amount else 0 end) as Stock
from
(
select nd,name,vdate,amount,'in' as Type from tmp_input
union
select nd,name,vdate,amount,'out' as type from tmp_output
)
a where vdate<='2005-05-30'
group by nd,name
) d
left outer join
(
select nd,name,sum(amount) as InQty
from tmp_input
where vdate>='2005-05-01' and vdate<='2005-05-31'
group by nd,name
) e on d.nd=e.nd
left outer join
(
select nd,name,sum(amount) as OutQty
from tmp_output
where vdate>='2005-05-01' and vdate<='2005-05-31'
group by nd,name
)f on d.nd=f.nd
select d.nd,d.name,d.Stock,e.inQty,f.outqty,g.stock as PrevStock
from (
select nd,name,
sum(case type when 'in' then amount else 0 end)-sum(case type when 'out' then amount else 0 end) as Stock
from
(
select nd,name,vdate,amount,'in' as Type from tmp_input
union
select nd,name,vdate,amount,'out' as type from tmp_output
)
a where vdate<='2005-05-30'
group by nd,name
) d
left outer join
(
select nd,name,sum(amount) as InQty
from tmp_input
where vdate>='2005-05-01' and vdate<='2005-05-31'
group by nd,name
) e on d.nd=e.nd
left outer join
(
select nd,name,sum(amount) as OutQty
from tmp_output
where vdate>='2005-05-01' and vdate<='2005-05-31'
group by nd,name
)f on d.nd=f.ndleft outer join
(
select nd,name,
sum(case type when 'in' then amount else 0 end)-sum(case type when 'out' then amount else 0 end) as Stock
from
(
select nd,name,vdate,amount,'in' as Type from tmp_input
union
select nd,name,vdate,amount,'out' as type from tmp_output
)
g1 where vdate<='2005-04-30'
group by nd,name
) g
on d.nd=g.nd
icedut,谢谢你,按照你的代码,我先研究一下去。。
MSN: [email protected]
我是一个VB初学者,
请教怎样写一个的语句,使用变量代替语句中的日期?