社区
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表中的公式用户自定义,数量不限,请大家帮忙解决一下,谢谢
...全文
208
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写文章
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
打赏
举报
回复
有點難度
Excel 2003 之入门教程
在Word中也有表格,在Excel表格与W表格的最大不同在于Excel表格具有强大的数字
运算
和数字分析能力.Excel中内置的
公式
和函数,可能
帮忙
用户进行复杂的计算.由于E在数据
运算
方面有强大的功能,使它成为用户办公必不可少...
excel的使用
如果用得到,你还可以利用EXCEL来完成行列式、矩阵的各种计算,进行简单的积分
运算
,利用迭代求函数值(如x^2=x^7+4,可用迭代方法求x值),等等,凡是涉及计算方面的事,找EXCEL来
帮忙
,它一定会给你一个满意的答案...
excel函数
公式
html文档,Excel中把计算式转换为
运算
结果的方法 EXCEL中计算出的
公式
如何转换成纯文本内容?...
如何在EXCEL中将计算
公式
直接变结果如我在A1输入计算式:(1+2*3+4/5-6)*1.2+(1+5) 如何在B1得出该计算式的选中B1然后按...在Excel中的
公式
运算
结果如何转换为文本格式。我想将
运算
结果拷贝到其他地方。方法一: ...
C语言入门——取余
运算
C语言入门——简单的取余
运算
学习C语言时,我们可能会遇到%,在平常的数学或...有了它我们可以解决一些简单的取余
问题
后面如果要学快速幂的话,了解一些重要的取余
运算
性质,无疑是有帮助的 (a + b) % c = (a % c + b
原码、反码、补码、位
运算
精讲
首先,在学习高级语言时,我们都有一个疑问,高级语言为什么还要保留位
运算
这种“低级”操作?我们都知道存储单元是CPU访问存储器的最基本单位,每个存储单元由8个二进制位组成,每个二进制位能存一个0或1。这...
MS-SQL Server
34,590
社区成员
254,589
社区内容
发帖
与我相关
我的任务
MS-SQL Server
MS-SQL Server相关内容讨论专区
复制链接
扫一扫
分享
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章