问一个问题

zhongwancheng 2003-10-28 09:14:28
表中有记录如下:
产品名称 进 出
A 20 0
B 30 0
A 0 15
B 15 0
A 0 5
A 5 0
A 1 0
查询数据时:
得出库存SUM(进-出)为 6但我要得到的数据是
(20-15-5)=0之后的两条数据
A 5 0
A 1 0

因为数据量很大,我要每次得到的都是(前面和为0)之后的库存的记录??

...全文
52 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
zarge 2003-10-29
  • 打赏
  • 举报
回复
如果有流水号如id,可以这样

select x.* from 表 x inner join
(
select max(id) as id, 产品名称 from 表 t
where (select sum(进)-sum(出) from 表 where id<=t.id and 产品名称=t.产品名称) = 0
group by all 产品名称
) y on x.产品名称 = y.产品名称 and x.id > isnull(y.id, 0)
order by x.id
zhongwancheng 2003-10-29
  • 打赏
  • 举报
回复
zjcxc(邹建)
不好意思,嵌套我真的看不明白.
可以一步步吗>?
非常的不好意思.
gmlxf 2003-10-29
  • 打赏
  • 举报
回复
顺序我想一定是会有的,考虑了一些时间,还是觉的好麻烦。等待高手来解决。
用cursor找出分界线的id号,然后选择后面的。
zhongwancheng 2003-10-29
  • 打赏
  • 举报
回复
好象没有找出sum(进-出) =0 日期啊??
zjcxc 2003-10-29
  • 打赏
  • 举报
回复
--定义表结构(用表变量的方法,@tb相当于一个表)
declare @tb table(产品名称 varchar(10),进 int,出 int,日期 datetime)
--插入数据到表中
insert into @tb
select 'A',20,0,'2003-10-2 08:30'
union all select 'B',30,0,'2003-10-2'
union all select 'A',0,15,'2003-10-3 08:20'
union all select 'B',15,0,'2003-10-3'
union all select 'A',0,5,'2003-10-3 09:30'
union all select 'A',5,0,'2003-10-3 09:50'
union all select 'A',1,0,'2003-10-4 10:20'

--从表中查询出数据,用了嵌套子查询的方法
select *
,结余=(select sum(进-出) from @tb where 产品名称=a.产品名称 and 日期<=a.日期)
from @tb a

zhongwancheng 2003-10-29
  • 打赏
  • 举报
回复
zjcxc(邹建)
不好意思,我太菜啦,我看不明白.

如果日期字段不重复,可以用下面的方法,否则用大力的方法
declare @tb table(产品名称 varchar(10),进 int,出 int,日期 datetime)
insert into @tb
select 'A',20,0,'2003-10-2 08:30'
union all select 'B',30,0,'2003-10-2'
union all select 'A',0,15,'2003-10-3 08:20'
union all select 'B',15,0,'2003-10-3'
union all select 'A',0,5,'2003-10-3 09:30'
union all select 'A',5,0,'2003-10-3 09:50'
union all select 'A',1,0,'2003-10-4 10:20'

select *
,结余=(select sum(进-出) from @tb where 产品名称=a.产品名称 and 日期<=a.日期)
from @tb a
可以解释一下吗??
zjcxc 2003-10-29
  • 打赏
  • 举报
回复
--如果有时间的话,就可以这样写啊,我不是给了例子了吗?

select *,结余=(select sum(进-出) from 表 where 产品名称=a.产品名称 and 日期<=a.日期) from 表 a
perfwell 2003-10-29
  • 打赏
  • 举报
回复

这样做吧!!!

declare @tb table(产品名称 varchar(10),进 int,出 int,日期 datetime)
insert into @tb
select 'A',20,0,'2003-10-2 08:30'
union all select 'B',30,0,'2003-10-2'
union all select 'A',0,15,'2003-10-3 08:20'
union all select 'B',15,0,'2003-10-3'
union all select 'A',0,5,'2003-10-3 09:30'
union all select 'A',5,0,'2003-10-3 09:50'
union all select 'A',1,0,'2003-10-4 10:20'

select *
,结余=(select sum(进-出) from @tb where 产品名称=a.产品名称 and 日期<=a.日期)
from @tb a
zhongwancheng 2003-10-29
  • 打赏
  • 举报
回复
to : pengdali(大力 V3.0) ( )

日期可以加时间的.
加了时间怎么样作呢>?????


zhongwancheng 2003-10-29
  • 打赏
  • 举报
回复
to : pengdali(大力 V3.0) ( )

日期可以加时间的.
zjcxc 2003-10-29
  • 打赏
  • 举报
回复
--如果日期字段不重复,可以用下面的方法,否则用大力的方法
declare @tb table(产品名称 varchar(10),进 int,出 int,日期 datetime)
insert into @tb
select 'A',20,0,'2003-10-2 08:30'
union all select 'B',30,0,'2003-10-2'
union all select 'A',0,15,'2003-10-3 08:20'
union all select 'B',15,0,'2003-10-3'
union all select 'A',0,5,'2003-10-3 09:30'
union all select 'A',5,0,'2003-10-3 09:50'
union all select 'A',1,0,'2003-10-4 10:20'

select *
,结余=(select sum(进-出) from @tb where 产品名称=a.产品名称 and 日期<=a.日期)
from @tb a
pengdali 2003-10-29
  • 打赏
  • 举报
回复
他的日期如果只有日期,没有时间一样没用。
zarge 2003-10-29
  • 打赏
  • 举报
回复
有日期就这样试试

select x.* from 表 x inner join
(
select max(日期) as 日期, 产品名称 from 表 t
where (select sum(进)-sum(出) from 表 where 日期<=t.日期 and 产品名称=t.产品名称) = 0
group by all 产品名称
) y on x.产品名称 = y.产品名称 and datediff(d, x.日期, isnull(y.日期, 0)) < 0
order by x.日期
pengdali 2003-10-29
  • 打赏
  • 举报
回复
create table #你的表(产品名称 varchar(10),进 int,出 int)
insert #你的表 values('A', 20 , 0)
insert #你的表 values('B', 30 , 0)
insert #你的表 values('A', 0 , 15)
insert #你的表 values('B', 15 ,0)
insert #你的表 values('A', 0 ,5)
insert #你的表 values('A', 5 ,0)
insert #你的表 values('A', 1 ,0)

declare @a int,@b int
select @a=0,@b=0
select *,0 flag into #b from #你的表 where 产品名称='A'

update #b set @a=@a+进-出,@b=case when @a=0 then @b+1 else @b end,flag=case when @a=0 then -@b else @b end

select 产品名称,进,出 from #b where flag=@b

go
drop table #你的表,#b
zhongwancheng 2003-10-29
  • 打赏
  • 举报
回复
各位大侠,我忘记了说的事还有一个时间字段,就是说每插入一条新记录的时间都是增加的.
这样好做吗??
产品名称 进 出 日期
A 20 0 2003-10-2
B 30 0 2003-10-2
A 0 15 2003-10-3
B 15 0 ......
A 0 5 .......
A 5 0 2003-10-3
A 1 0 2003-10-4
tailzhou 2003-10-29
  • 打赏
  • 举报
回复
搂主说清楚些吧
1)是不是输出最后的sum(进-出)=0的纪录之后的纪录?
产品名称 进 出
1。A 20 0
2。A 0 20
3。A 1 0
4。A 0 1
5。A 10 0
6。A 5 0

是输出3,5,6还是只输出5,6?

2)没有sum(进-出)=0的条件时希望输出什么?

1。A 20 0
2。A 0 15
3。A 15 0
4。A 5 0
输出3,4?
liuyun2003 2003-10-28
  • 打赏
  • 举报
回复
即使有顺序也很难做啊。我关注高手解决。
jkljf 2003-10-28
  • 打赏
  • 举报
回复
要是你的记录都是按照顺序来的或者有个时间戳就好了, 这样就可以做了,否则我觉得很难下手阿

22,294

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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