有出库表和入库表,求库存的问题

bing475879749 2009-09-15 04:44:18
入库表(ruku):
classname num overtime(过期日期)
名字1 55 2009-12-12
名字1 99 2009-09-12
名字2 33 2009-11-12
名字3 66 2009-10-12
名字3 44 2009-12-00

出库表(chuku):
classname num
名字1 120
名字2 30
名字3 100

用SQL语句查出库存,过期日期排前的要先出库
classname num overtime
名字1 34 2009-12-12
名字1 0 2009-09-12
名字2 3 2009-11-12
名字3 0 2009-10-12
名字3 10 2009-12-00
...全文
413 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
modie_832 2010-03-27
  • 打赏
  • 举报
回复
如果加上包装规格和仓库,该怎么样的算库存?
ChinaJiaBing 2009-09-16
  • 打赏
  • 举报
回复


declare @a table (classname nvarchar(10),num int,overtime datetime)
insert into @a select '名字1',55,'2009-12-12'
union all select '名字1',99,'2009-09-12'
union all select '名字2',33,'2009-11-12'
union all select '名字3',66,'2009-10-12'
union all select '名字3',44,'2009-12-1'
declare @b table (classname nvarchar(10),num int)
insert into @b select '名字1',120
union all select '名字2',30
union all select '名字3',100
; with china as
(
select classname,SUM(num)s,MAX(overtime)m from @a
group by classname


(
select classname,s=s-(select SUM(num) from @b b where a.classname=b.classname )
,m from china a
)
select isnull(a.classname,b.classname) classname,
a.s num,
isnull(a.m,b.overtime) overtime
from china1 a right join @a b on a.classname=b.classname
and a.m=b.overtime


classname num overtime
---------- ----------- -----------------------
名字1 34 2009-12-12 00:00:00.000
名字1 NULL 2009-09-12 00:00:00.000
名字2 3 2009-11-12 00:00:00.000
名字3 NULL 2009-10-12 00:00:00.000
名字3 10 2009-12-01 00:00:00.000

(5 行受影响)

gsk09 2009-09-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 gsk09 的回复:]
SQL codedeclare@rukutable ([classname]varchar(5),[num]int,[overtime]varchar(10))insertinto@rukuselect'名字1',55,'2009-12-12'unionallselect'名字1',99,'2009-09-12'unionallselect'名字2',33,'2009-11-12'unionall¡­
[/Quote]

???


declare @ruku table ([classname] varchar(5),[num] int,[overtime] varchar(10))
insert into @ruku
select '名字1',55,'2009-12-12' union all
select '名字1',99,'2009-09-12' union all
select '名字2',33,'2009-11-12' union all
select '名字3',66,'2009-10-12' union all
select '名字3',44,'2009-12-00'
--> Test data : @chuku
declare @chuku table ([classname] varchar(5),[num] int)
insert into @chuku
select '名字1',120 union all
select '名字2',1 union all
select '名字2',1 union all
select '名字2',1 union all
select '名字2',1 union all
select '名字2',1 union all
select '名字2',1 union all
select '名字2',1 union all
select '名字2',1 union all
select '名字2',1 union all
select '名字2',1 union all
select '名字2',1 union all
select '名字2',1 union all
select '名字2',1 union all
select '名字2',1 union all
select '名字2',1 union all
select '名字3',100

--select * from @ruku
--select * from @chuku

select
classname
,((sign(num)+1)/2)*num num
,overtime
from
(select
r.classname
, (select sum(r2.num) from @ruku r2 where r.classname = r2.classname and r.[overtime]>=r2.[overtime])
-(select sum(num) from @chuku c where r.classname = c.classname ) num
,r.overtime
from @ruku r
)as t

classname num overtime
--------- ----------- ----------
名字1 34 2009-12-12
名字1 0 2009-09-12
名字2 18 2009-11-12
名字3 0 2009-10-12
名字3 10 2009-12-00

(5 行)
zs621 2009-09-15
  • 打赏
  • 举报
回复
如果出库表的数据不只一条:


select a.className,a.overtime,
case when (select sum(num) from 入库表 where className=a.className and overtime<=a.overtime)<=b.num
then 0 when (select sum(num) from 入库表 where className=a.className and overtime<a.overtime)>b.num then a.num
else (select sum(num) from 入库表 where className=a.className and overtime<=a.overtime)-b.num
end
from 入库表 a left join (select sum(num) num,classname from 出库表 group by classname)b on a.className=b.classname
zs621 2009-09-15
  • 打赏
  • 举报
回复

select a.className,a.overtime,
case when (select sum(num) from 入库表 where className=a.className and overtime<=a.overtime)<=b.num
then 0 when (select sum(num) from 入库表 where className=a.className and overtime<a.overtime)>b.num then a.num
else (select sum(num) from 入库表 where className=a.className and overtime<=a.overtime)-b.num
end
from 入库表 a left join 出库表 b on a.className=b.classname


如果出库表的数据不只一条 自己改吧
bing475879749 2009-09-15
  • 打赏
  • 举报
回复
哎,LS几位的都有些片面,问题是我出库的记录不止一次
soft_wsx 2009-09-15
  • 打赏
  • 举报
回复
/*
Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) Jul 9 2008 14:43:34 Copyright (c)
1988-2008 Microsoft Corporation Enterprise Evaluation Edition on Windows NT 5.1 <X86>
(Build 2600: Service Pack 3)
愿和大家共同进步
如有雷同、实属巧合
●●●●●2009-09-15 17:22:18.827●●●●●
 ★★★★★soft_wsx★★★★★
*/


if OBJECT_ID('入库表') is not null drop table 入库表
go
create table 入库表(classname nvarchar(20), num int, overtime nvarchar(10))
go
insert into 入库表
select
'名字1', 55, '2009-12-12' union all select
'名字1', 99, '2009-09-12' union all select
'名字2', 33, '2009-11-12' union all select
'名字3', 66, '2009-10-12' union all select
'名字3', 44, '2009-12-00'

if OBJECT_ID('出库表') is not null drop table 出库表
go
create table 出库表(classname nvarchar(20), num int)
go
insert into 出库表
select
'名字1', 120 union all select
'名字2', 30 union all select
'名字3', 100

select a.classname,num=CAse when a.num<0 then 0 else a.num end,a.overtime
from(
select a.classname,num=
(select SUM(num) from 入库表 where classname=a.classname and overtime<=a.overtime)-b.num,
a.overtime
from 入库表 a left join 出库表 b on a.classname=b.classname
)a

/*
classname num overtime
名字1 34 2009-12-12
名字1 0 2009-09-12
名字2 3 2009-11-12
名字3 0 2009-10-12
名字3 10 2009-12-00
*/
gsk09 2009-09-15
  • 打赏
  • 举报
回复
classname num overtime
--------- ----------- ----------
名字1 34 2009-12-12
名字1 0 2009-09-12
名字2 3 2009-11-12
名字3 0 2009-10-12
名字3 10 2009-12-00
gsk09 2009-09-15
  • 打赏
  • 举报
回复

declare @ruku table ([classname] varchar(5),[num] int,[overtime] varchar(10))
insert into @ruku
select '名字1',55,'2009-12-12' union all
select '名字1',99,'2009-09-12' union all
select '名字2',33,'2009-11-12' union all
select '名字3',66,'2009-10-12' union all
select '名字3',44,'2009-12-00'
--> Test data : @chuku
declare @chuku table ([classname] varchar(5),[num] int)
insert into @chuku
select '名字1',120 union all
select '名字2',30 union all
select '名字3',100

--select * from @ruku
--select * from @chuku

select
classname
,((sign(num)+1)/2)*num
,overtime
from
(select
r.classname
, (select sum(num) from @ruku r2 where r.classname = r2.classname and r.[overtime]>=r2.[overtime])
-(select sum(num) from @chuku c where r.classname = c.classname ) num
,r.overtime
from @ruku r
)as t



--小F-- 2009-09-15
  • 打赏
  • 举报
回复
select a.classname,isnull(sum(a.num)-b.num,0)
from
ruku a
left join
chuku b
on a.classname=b.classname
group by a.classname,b.num
rucypli 2009-09-15
  • 打赏
  • 举报
回复
select T1.classname,T1.m-T2.num,T1.n
from (
select classname,sum(num) as m,max(overtime) as n
from ruku
grou by classname
)T1,chuku T2
where T1.classname=T2.classname
bing475879749 2009-09-15
  • 打赏
  • 举报
回复
库存是用 入库表—出库表 并排序等操作得到的
维尔科技推出的小型仓储/仓库/进销存管理工具采用Excel数据库引擎技术,按照软件工程思路设计开发,运行稳定,简单实用。不需要其他额外文件,不用做任何系统上的安装和设置。具有完整的仓库管理功能,如入出库管理功能、库存管理功能、货位管理功能。能实现按照生产日期先进先出,能够核算库存成本、销售利润,能够统计日报、周报、月报,能够查询入出库履历。广泛适用于中小物流企业的仓库管理、各类生产企业的库存管理、各类商业企业的进销存管理。免费版可以随意使用,但不得作为商业用途转让或出售。 1、入库管理,包含入库的物料代码、名称、数量、生产日期、入库日期、货位 a、在录入入库数量后,系统会自动更新库存模块的库存数量 b、支持修改功能,如果入库数量录错,可以修改,修改后会自动更新库存。 c、先进先出功能通过入库日期和货位共同来管理 d、拣货信息栏目用于显示该入库记录被何时出库 2、出库管理,包含出库的物料代码、名称、数量、出库日期等信息 a、录入出库数量后,系统会直接按照生产日期先进先出原则,查找适合货位上的商品 b、找到相应货位后,从该货位减去出库数,同时减库存数量;如果数量不够,则再继续找其他货位。 c、标记拣货信息(从哪个货位、拣出多少数量) d、如果最终的库存数不够出货,则再出库对应记录上显示缺货数 e、可以修改出库数量,但只能增加,不能减少 3、库存功能 a、通过库存来设置商品基础信息:物料号、物料名称、单价、单位重量、单位体积等 b、入库或者出库操作的时候,会自动增加或减少库存,并自动计算库存金额、总重量、总体积等 4、报功能 a、先设置好报的日期范围,日期范围必须输入,且为正确日期范围 b、日期范围如果设置成一月的第一天和最后一天,则报结果是月报,如果是一周的第一天,则是周报。 也可以设置成起始日期和结束日期为同一天,则是月报。 c、点击【统计】按钮,则自动统计出设定日期范围内各种商品的期初数、入库数、出库数、期末数等信息 5、查询功能 查询指定日期范围内,某种商品的入库出库履历。 6、发行版本说明 a、【入出库免费版】,实现入库管理、出库管理、自动汇总库存;不包含报功能、查询功能,不包含价格、重量、体积、成本、收入、利润等内容。 b、【仓库管理标准版】,支持在一台机器上注册;实现入出库管理、自动汇总库存;管理生产日期批次号、货位并实现按生产日期批次号先进先出;日报月报功能、入出库履历查询功能,单位、规格、单价、金额、重量、体积等扩展内容 c、【仓库管理公司版】,支持在多台机器上注册;实现入出库管理、自动汇总库存;管理生产日期批次号、货位并实现按生产日期批次号先进先出;日报月报功能、入出库履历查询功能,单位、规格、单价、金额、重量、体积等扩展内容。 d、【进销存公司版】,支持在多台机器上注册;实现采购入库和销售出库管理、自动汇总库存;管理采购价格、出库价格,库存成本按照移动平均核算,自动计算销售利润;允许设置默认入出库价格,允许设置期初库存和期初价格,允许随意修改入出库条目;日报月报功能、入出库履历查询功能,单位、规格、单价、金额、重量、体积等扩展内容。(更多功能敬请期待:后续版本将支持制单和打印操作)

27,579

社区成员

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

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