社区
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表中的公式用户自定义,数量不限,请大家帮忙解决一下,谢谢
...全文
211
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
加法结合律的
公式
可以表示为:(a+b)+c = a+(b+c)。这意味着无论是先计算括号内的加法,还是先计算括号外的加法,最终的结果都是一样的。 在实际教学中,我们可以通过一系列的例子来帮助学生理解和掌握加法结合律。...
Excel 2003 之入门教程
在Word中也有表格,在Excel表格与W表格的最大不同在于Excel表格具有强大的数字
运算
和数字分析能力.Excel中内置的
公式
和函数,可能
帮忙
用户进行复杂的计算.由于E在数据
运算
方面有强大的功能,使它成为用户办公必不可少...
小学数学顺口溜-整理.doc
乘法分配律的顺口溜“分配律来
帮忙
,括号拆开乘积加”,则在孩子头脑中形成了一个清晰的数学图像,将复杂的乘法分配
问题
简化。此外,除法的性质、等式和方程的概念,以及分数的加减法和比较大小的方法,都是孩子们...
2013腾讯编程马拉松初赛(3月21)赛题
其次,“小明系列故事——师兄帮
帮忙
”需要参赛者利用数学
公式
进行递推,这不仅考验了他们的数学知识,还考察了他们将数学
问题
转化为编程
问题
的能力。参赛者需要编写出能够快速计算出序列值的程序,并确保在规定时间...
小学数学数学故事数学童话北游记30免费的苹果
这位老伯有一篮子苹果,他希望八戒和悟空
帮忙
解决一个
问题
:他卖出了若干苹果,但现在的账目似乎有些混乱,他不确定自己是否算错了账。 八戒决定用数学知识帮助老伯。他首先应用了逆向思维的方法,从老伯目前拥有的...
MS-SQL Server
34,838
社区成员
254,632
社区内容
发帖
与我相关
我的任务
MS-SQL Server
MS-SQL Server相关内容讨论专区
复制链接
扫一扫
分享
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章