公式运算问题,请大家帮忙

xlfd1999 2005-07-04 03:50:11
有两个表,table1是用来保存用户自定义公式,有两个字段
hc(行次),gs(公式)
1,A+B
2,A+C
3,A+C-B
......
table2是具体数据,
ID,num
A,1000
B,2000
C,3000
我想根据table1中的公式查询table2表中的数据,得到的结果是:
1,3000
2,4000
3,2000
......
table1表中的公式用户自定义,数量不限,请大家帮忙解决一下,谢谢
...全文
208 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
子陌红尘 2005-07-05
  • 打赏
  • 举报
回复
libin_ftsafe(子陌红尘)你所说的"只能处理小数据量的情况。"的小数据量的范围是多少呀?
---------------------------------------------------------------------------------
保守估计也就100--200条记录吧。
xlfd1999 2005-07-05
  • 打赏
  • 举报
回复
libin_ftsafe(子陌红尘)你所说的"只能处理小数据量的情况。"的小数据量的范围是多少呀?
xlfd1999 2005-07-05
  • 打赏
  • 举报
回复
非常感谢libin_ftsafe(子陌红尘)的鼎力相助,我想进一步再请教个问题,如果公式表(table1)中的公式有这样两行:
5,#(1+2+3) [这代表查询结果中行1、行2、行3值相加,也就是9000]
6,NULL [这代表行6里面没有公式,不用计算]
这时该会何处理呢?
hglhyy 2005-07-05
  • 打赏
  • 举报
回复
是啊,临时表用完删除啊!
子陌红尘 2005-07-05
  • 打赏
  • 举报
回复
libin_ftsafe(子陌红尘) 谢谢你的帮忙
你的方法我在执行多次的时候,它会报以下错误:

服务器: 消息 2714,级别 16,状态 6,行 1
数据库中已存在名为 '##t' 的对象。
------------------------------------------------------------------------

呵呵,楼主难道没看出来 ##t 它是个全局临时表吗?用完之后删除掉就可以了:

create table table1(hc int,gs varchar(20))
insert into table1 select 1,'A+B'
insert into table1 select 2,'A+C'
insert into table1 select 3,'A+C-B'
insert into table1 select 4,'C-A-B'

create table table2(ID varchar(20),num int)
insert into table2 select 'A',1000
insert into table2 select 'B',2000
insert into table2 select 'C',3000


--执行查询
declare @s varchar(8000)
set @s = ''

select @s = @s+',['+ID+']=sum(case ID when '''+ID+''' then num end)' from table2

set @s = stuff(@s,1,1,'')
exec('select '+ @s + ' into ##t from table2')

set @s = ''
select @s = @s + ' union all select '+rtrim(hc)+','+gs+' from ##t' from table1
set @s = stuff(@s,1,10,'')
exec(@s)

drop table ##t

xlfd1999 2005-07-05
  • 打赏
  • 举报
回复
libin_ftsafe(子陌红尘) 谢谢你的帮忙
你的方法我在执行多次的时候,它会报以下错误:

服务器: 消息 2714,级别 16,状态 6,行 1
数据库中已存在名为 '##t' 的对象。
xlfd1999 2005-07-05
  • 打赏
  • 举报
回复
evlon(阿牛) 正如你所说,生成一个存储过程,以后多次执行查询
evlon 2005-07-04
  • 打赏
  • 举报
回复
感觉楼主这样的东西不是每次都要生成吧,如果是生成一次,使用多次的情况,就应该采用别的思路来解决这件事情了
子陌红尘 2005-07-04
  • 打赏
  • 举报
回复
当然,这样的写法有缺陷,即 @s 长度不能超过 8000 字节,只能处理小数据量的情况。
子陌红尘 2005-07-04
  • 打赏
  • 举报
回复
何错之有?
---------------------------------------------------------------生成测试数据
create table table1(hc int,gs varchar(20))
insert into table1 select 1,'A+B'
insert into table1 select 2,'A+C'
insert into table1 select 3,'A+C-B'
insert into table1 select 4,'C-A-B'

create table table2(ID varchar(20),num int)
insert into table2 select 'A',1000
insert into table2 select 'B',2000
insert into table2 select 'C',3000


--执行查询
declare @s varchar(8000)
set @s = ''

select @s = @s+',['+ID+']=sum(case ID when '''+ID+''' then num end)' from table2

set @s = stuff(@s,1,1,'')
exec('select '+ @s + ' into ##t from table2')

set @s = ''
select @s = @s + ' union all select '+rtrim(hc)+','+gs+' from ##t' from table1
set @s = stuff(@s,1,10,'')
exec(@s)


--输出结果
---- ------
1 3000
2 4000
3 2000
4 0
xlfd1999 2005-07-04
  • 打赏
  • 举报
回复
TO: libin_ftsafe(子陌红尘)
如果用户再自定义一条:
4,C-A-B
你的方法就报错了!
table1中的公式是用户自定义的,不知道长度和公式的样式.
子陌红尘 2005-07-04
  • 打赏
  • 举报
回复
--生成测试数据
create table table1(hc int,gs varchar(20))
insert into table1 select 1,'A+B'
insert into table1 select 2,'A+C'
insert into table1 select 3,'A+C-B'

create table table2(ID varchar(20),num int)
insert into table2 select 'A',1000
insert into table2 select 'B',2000
insert into table2 select 'C',3000


--执行查询
declare @s varchar(8000)
set @s = ''

select @s = @s+',['+ID+']=sum(case ID when '''+ID+''' then num end)' from table2

set @s = stuff(@s,1,1,'')
exec('select '+ @s + ' into ##t from table2')

set @s = ''
select @s = @s + ' union all select '+rtrim(hc)+','+gs+' from ##t' from table1
set @s = stuff(@s,1,10,'')
exec(@s)


--输出结果
1 3000
2 4000
3 2000
子陌红尘 2005-07-04
  • 打赏
  • 举报
回复
--生成测试数据
create table table1(hc int,gs varchar(20))
insert into table1 select 1,'A+B'
insert into table1 select 2,'A+C'
insert into table1 select 3,'A+C-B'

create table table2(ID varchar(20),num int)
insert into table2 select 'A',1000
insert into table2 select 'B',2000
insert into table2 select 'C',3000


--执行查询
declare @s varchar(8000)
set @s = ''

select @s = @s+',['+ID+']=sum(case ID when '''+ID+''' then num end)' from table2

set @s = stuff(@s,1,1,'')
exec('select '+ @s + ' into ##t from table2')

set @s = ''
select @s = @s + ' union all select '+rtrim(hc)+','+gs+' from ##t' from table1
set @s = stuff(@s,1,10,'')
exec(@s)


--输出结果
1 3000
2 4000
3 2000
Stone444 2005-07-04
  • 打赏
  • 举报
回复
有點難度

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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