金额合计求值问题
来源:互联网 发布:java object转换成map 编辑:程序博客网 时间:2024/05/03 20:27
原帖地址:
http://community.csdn.net/Expert/topic/3190/3190686.xml?temp=.6296961
表test中记录:
aa bb
001 50.5
002 60
003 15.4
004 25
005 48
...
输入任一金额,然后在表中查找是否有该金额或几条记录的合计等于该金额.
如:输入25,则要找出004,输入85,则要找出002与004,依次类推。
------------------------------------------------------------------------------------
--测试数据
create table test(aa varchar(10),bb numeric(10,2))
insert test select '001',50.5
union all select '002',60
union all select '003',15.4
union all select '004',25
union all select '005',48
union all select '006',37
go
--查询函数
create function fn_search(@Num numeric(10,2))
returns @r table (aa varchar(10),bb numeric(10,2))
as
begin
declare @t table (aa varchar(8000),aa1 varchar(10),bb numeric(10,2),level int)
declare @l int
insert @r select aa,bb from test where bb=@num
if @@rowcount>0 goto lb_exit
set @l=0
insert @t select ','+aa+',',aa,bb,@l from test where bb<@num
while @@rowcount>0
begin
insert @r select distinct a.aa,a.bb
from test a,(
select a.aa,a.bb,aa1=b.aa from test a,@t b
where b.level=@l
and b.aa1 and a.bb=@num-b.bb
)b where a.aa=b.aa or charindex(','+a.aa+',',b.aa1)>0
if @@rowcount>0 goto lb_exit
set @l=@l+1
insert @t select b.aa+a.aa+',',a.aa,a.bb+b.bb,@l
from test a,@t b
where b.level=@l-1
and b.aa1 and a.bb<@num-b.bb
end
lb_exit:
return
end
go
--调用测试1
select * from dbo.fn_search(25)
/*--结果
aa bb
---------- ------------
004 25.00
(所影响的行数为 1 行)
--*/
--调用测试2
select * from dbo.fn_search(135.5)
/*--结果
aa bb
---------- ------------
001 50.50
002 60.00
004 25.00
005 48.00
006 37.00
(所影响的行数为 5 行)
--*/
--调用测试3(找不到的,无返回值)
select * from dbo.fn_search(135.7)
/*--结果
aa bb
---------- ------------
(所影响的行数为 0 行)
--*/
go
drop table test
drop function fn_search
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=67518
- 金额合计求值问题
- 金额合计求值问题
- 金额合计求值问题
- 金额合计求值问题
- 金额合计求值问题
- 金额合计求值问题
- 金额合计求值问题
- 金额合计求值问题
- 金额合计求值问题
- 金额合计求值问题
- 金额合计求值问题
- 金额合计求值问题
- 金额合计求值问题
- 金额合计求值(返回总计值为指定数的集合)
- 合计金额计算
- 金额合计方法
- <c:forEach> 合计金额
- 合计汇总金额SQl
- LifeStreams:跟踪你所用的服务之所有RSS Feed
- 玩聚互联网热点新发现精度上升到了一个新高度
- 职场笔记[0705]:雄才大略、靠什么竞争?
- 【新媒体讨论】关联趋势和“就是不服”
- 亚马逊畅销测试图书《软件测试与持续质量改进》译者序
- 金额合计求值问题
- 【职场】帮你.帮我和Show. Me. The. Money.
- Web2.0-人性驱动的事业和仿生学
- 《Web性能测试实战》性能测试计划模板
- 劳心劳力的中国匠人文化
- [职场生存]细节和感觉[一]:细节
- 技术英雄会【四】:也谈如何发掘到需要的内容和英雄
- 单表导入/导出文本文件
- 《LoadRunner性能测试实战》第五章开头部分