楼上的没有仔细看吧?按ID的顺序确定优先级别,按stocknum字段的值最大话满足,例子里给定的是待分配的数是35,ID号为1 的stocknum是20,所以把35中的20分给它,然后是ID号为2,stocknum是10,剩下是15,就分给它10,最后ID为3的,虽然为30,但只剩下5了,所以分给它的是5,所有的分配都放在lastnum的列上,这样说不知道明白没有?我要求一条语句实现这样的分配不知道行不,否则也给出其他方法也可,谢谢
解决方案 »
- SQL分组问题
- select into占用内存
- 个人版的SqlServer2000与企业版的SqlServer有什么区别?
- 一个新库一个旧库中的同一个表,怎么汇总表中字段的比较情况,用什么好办法
- 链接数据库时报错!
- 关于Analysis Manager里面的MDX示例无法连接的问题!
- sql2000企业版 数据库文件过大怎么办
- 问sql server的几个概念问题
- 现有一记录表,表名为tabTest,其字段名为id,c1,c2,c3,c4,c5,现这个表有5笔记录
- 要动态把edit1.text的值作为数据库名怎么做。急.
- 如何利用textcopy将stdinfo表中的photo列的学生照片全部生成bmp位图保存?
- 当一个表字段很多时,我只有一个字段不想选,请问有没有简单一点的方法实现(也就是语句不用写出所有要选的字段名)
insert into #t
select 20,0 union all
select 10,0 union all
select 30,0update a set lastnum=(case when (select sum(stocknum) from #t where id<=a.id)>=35 then 35-stocknum else stocknum end) from #t a
select * from #t--结果
/*
id stocknum lastnum
----------- ----------- -----------
1 20 20
2 10 10
3 30 5
*/
create table #t(id int identity(1,1),stocknum int,lastnum int)
insert into #t
select 20,0 union all
select 33,0 union all
select 30,0update a
set lastnum=(case when (select sum(stocknum) from #t where id<=a.id)>=35 then case when 35>=(select sum(stocknum) from #t where id<a.id) then 35-(select sum(stocknum) from #t where id<a.id) else 0 end else stocknum end)
from #t aselect * from #t