社区
MS-SQL Server
帖子详情
公式运算问题,请大家帮忙
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表中的公式用户自定义,数量不限,请大家帮忙解决一下,谢谢
...全文
214
14
打赏
收藏
公式运算问题,请大家帮忙
有两个表,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表中的公式用户自定义,数量不限,请大家帮忙解决一下,谢谢
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用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
打赏
举报
回复
有點難度
新北师大版数学四年级上册第四单元
运算
律第4课时加法结合律教案.pdf
新北师大版数学四年级上册第四单元
运算
律第4课时加法结合律教案.pdf
EXCEL入门教程.ppt
Excel是Microsoft公司推出的办公软件Office中的一个重要组成成员,也是目前最流行的关于电子表格处理的软件之一.它具有强大的计算、分析和图表等功能,是公司目前最常用的办公数据表格软件。 在Word中也有表格,在Excel表格与W表格的最大不同在于Excel表格具有强大的数字
运算
和数字分析能力.Excel中内置的
公式
和函数,可能
帮忙
用户进行复杂的计算.由于E在数据
运算
方面有强大的功能,使它成为用户办公必不可少的一个常用办公软件。
Excel 2003 之入门教程
Excel入门教程 Excel是Microsoft公司推出的办公软件Office中的一个重要组成成员,也是目前最流行的关于电子表格处理的软件之一.它具有强大的计算、分析和图表等功能,是公司目前最常用的办公数据表格软件。 在Word中也有表格,在Excel表格与W表格的最大不同在于Excel表格具有强大的数字
运算
和数字分析能力.Excel中内置的
公式
和函数,可能
帮忙
用户进行复杂的计算.由于E在数据
运算
方面有强大的功能,使它成为用户办公必不可少的一个常用办公软件。
小学数学顺口溜-整理.doc
小学数学顺口溜-整理.doc
小学数学数学故事数学童话北游记30免费的苹果
小学数学数学故事数学童话北游记30免费的苹果
MS-SQL Server
34,871
社区成员
254,637
社区内容
发帖
与我相关
我的任务
MS-SQL Server
MS-SQL Server相关内容讨论专区
复制链接
扫一扫
分享
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章