意思就是有一张表,记录的是一本书<html>的信息,它们是一样的书,但是批号不一样,如果有人来买书的话,那我要先卖那些先进来的书,就是按我上面的顺序从上往下卖,如果有人要买10本或者小于10本,那我的第一条记录的数量就可以满足了,我只要查询出第一条记录
如果有人要买15本,那我第一条记录只有10本,所以我现在要卖第一条记录的10本还有第二条记录的5本,我要查询出前两条记录。
如果有人要买35本,那我前两条记录加起来才30本不够,那我就要查出加起来大于35本的那几条记录
我的意思能明白么,谢谢了。
如果有人要买15本,那我第一条记录只有10本,所以我现在要卖第一条记录的10本还有第二条记录的5本,我要查询出前两条记录。
如果有人要买35本,那我前两条记录加起来才30本不够,那我就要查出加起来大于35本的那几条记录
我的意思能明白么,谢谢了。
解决方案 »
- A 存储过程调用B 存储过程,在 A过程 中如何实现"事物处理"呢?
- 老问题,好像是我没说明白???
- 为什么这段执行后显示不出结果来?
- 求高手帮忙写一条select语句
- sql语句不能用外连接
- 问大家一个简单的select语句
- 寻个跨主机存储高手,,急,请大仙帮忙!
- 能不能帮我做下这个题目啊……谢谢啊…………救命啊……
- 求助:自定义函数不支持exec的变通解决办法
- 查询两个表时间段重复的记录
- 这个问题难不难啊?帮忙看一下
- [Microsoft][ODBC SQL Server Driver][TCP/IP Sockets]ConnectionCheckForData (CheckforData()).
min(最小借书量)
select top ((max-min)/10) from tablename
declare @table table(xh int,name char(4),ph char(4),sl int)
--测试数据
insert @table select
1, 'html', '1111', 10 union all select
2 , 'html', '2222', 20 union all select
3 , 'html', '3333', 30 union all select
4 , 'html', '4444', 40 union all select
5 ,'html', '5555', 50
--实现
select b.xh,b.name,b.ph,b.sl from @table a join @table b
on a.xh <= b.xh
group by b.xh,b.name,b.ph,b.sl
having sum(a.sl) <= 30 --可以动态修改 /*
xh name ph sl
----------- ---- ---- -----------
1 html 1111 10
2 html 2222 20(所影响的行数为 2 行)
*/
declare @table table(xh int,name char(4),ph char(4),sl int)
--测试数据
insert @table select
1, 'html', '1111', 10 union all select
2 , 'html', '2222', 20 union all select
3 , 'html', '3333', 30 union all select
4 , 'html', '4444', 40 union all select
5 ,'html', '5555', 50
--实现
select * from @table
where xh <=
(select top 1 b.xh from @table a join @table b
on a.xh <= b.xh
group by b.xh,b.name,b.ph,b.sl
having sum(a.sl)>=35--可以动态修改
)
/*
xh name ph sl
----------- ---- ---- -----------
1 html 1111 10
2 html 2222 20
3 html 3333 30(所影响的行数为 3 行)
*/
declare @table table(xh int,name char(4),ph char(4),sl int)
--测试数据
insert @table select
1, 'html', '1111', 10 union all select
2 , 'html', '2222', 20 union all select
3 , 'html', '3333', 30 union all select
4 , 'html', '4444', 40 union all select
5 ,'html', '5555', 50select * from @table a,
(select xh,(select sum(sl) from @table where xh <= a.xh) sumsl from @table a) b
where a.xh = b.xh and b.sumsl <=10select * from @table a,
(select xh,(select sum(sl) from @table where xh <= a.xh) sumsl from @table a) b
where a.xh = b.xh and b.sumsl <=30select * from @table a,
(select xh,(select sum(sl) from @table where xh <= a.xh) sumsl from @table a) b
where a.xh = b.xh and b.sumsl <=60
xh name ph sl
1 'html' '1111' 10
2 'html' '2222' 20
3 'html' '3333' 30
4 'html' '4444' 40
5 'html' '5555' 50
建函数
create function testtest(@count int) returns int
as
begin
declare @xh int
set @xh=1
while 1=1
begin
if(select sum(sl) from tmp where xh<=@xh)>=@count
break
else
begin
set @xh=@xh+1
continue
end
end
return @xh
end执行语句
select * from tmp where xh<=dbo.test(40)
结果
1 html 1111 10
2 html 2222 20
3 html 3333 30
函数没有考虑总数少于要求数的情况,自己完善一下
试一下看是否支持