这样的“计算”应该怎样写?

starryconfig 2012-10-17 01:28:21
表“PhantomFormula”:

【MeterNo】(表具编号) 【Formula】(计算公式)
XB0301 XB0301=063457+063470+063464-063473
XD1203 XD1203=063462-063475

表“Meter”:
【MeterNo】(表具编号) 【RtQty】(值)
063453 875.7
063454 8485.7

问题:
“XB0301=063457+063470+063464+063473”这个是“XB0301(表具编号)”对应的“数据”的“计算公式”。 根据这个公式,从“Meter”表里面查出公式中对应的表具的“RtQty(值)”,并进行相应的“加、减、乘、除”,得到结果。

这样的计算过程用“SQL”应该怎样写??? 谢谢各位大师。

...全文
236 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
starrycheng 2012-11-19
  • 打赏
  • 举报
回复
大师,您怎么看呢???
發糞塗牆 2012-10-17
  • 打赏
  • 举报
回复
starrycheng 2012-10-17
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]
查询用到的函数 f_split
SQL code


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



CREATE function [dbo].[f_split](@c varchar(5000),@split varchar(2))
returns @t table(col varchar(5000)……
[/Quote]

谢谢大师,谢谢。 能不能简单的介绍下您的思路。
您的思路整体是:
(1)取出等号右边的表达式。“3+4-5”
(2)给符号加双引号。3"+"4"-"5
(3)根据"",取出“3、+、4、-、5”
(5)将3"+"4"-"5变为“333.00+444.00-555.00”
(6)关联“MeterNo”。

1、“f_split”函数是怎样将3"+"4"-"5变为“3、+、4、-、5”的呢?

2、“ if(ascii(@tmpstr)<>163)”这个判断时判断的什么?

3、“with cte_a as” 、 “cte_b as” 、“cte_c as” 、“cte_d as” ,这些是 with 的用法,可以理解为临时表,对吧?


4、“stuff((select '_'+cast (col as nvarchar(100)) from cte_d z where t.id=z.id for xml path('')), 1, 1, '')” 这一句的作用是什么呢? “for xml path('')”这是什么呀???


还请大师赐教。
IEEE_China 2012-10-17
  • 打赏
  • 举报
回复

查询用到的函数 f_split


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



CREATE function [dbo].[f_split](@c varchar(5000),@split varchar(2))
returns @t table(col varchar(5000))
as
begin
declare @tmpstr varchar
declare @index int
set @index=1;
while(charindex(@split,@c,@index)<>0)
begin
set @tmpstr=substring(@c,charindex(@split,@c,@index),charindex(@split,@c,@index))
if(ascii(@tmpstr)<>163)
begin
insert @t(col) values (substring(@c,1,charindex(@split,@c,@index)-1))
set @c = stuff(@c,1,charindex(@split,@c,@index),'')
set @index=1
end
else
begin
set @index=(charindex(@split,@c,@index)+1)
end
end
insert @t(col) values (@c)
return
end






查询脚本:

CREATE TABLE #PhantomFormula(
[ID] [int] IDENTITY(1,1) NOT NULL,
[MeterNo] [nvarchar](50) NULL ,
[Formula][nvarchar](50) NULL

)
CREATE TABLE #Meter(
[ID] [int] IDENTITY(1,1) NOT NULL,
[MeterNo][nvarchar](50) NULL ,
[RtQty] decimal(12,2) NULL

)
insert into #Meter
select '1',111.00 union all
select '2',222.00 union all
select '3',333.00 union all
select '4',444.00 union all
select '5',555.00 union all
select '6',666.00

insert into #PhantomFormula
select 'XB0301','XB0301=1+2-3' union all
select 'XB0302','XB0302=2+3+4' union all
select 'XB0303','XB0303=3+4-5' union all
select 'XB0304','XB0304=4+5+6'



declare @id int

set @id=3 ----当前要查询的 ID

----取表达式等号右边数据
;with cte_a as(
select ID,MeterNO, right(Formula, charindex('=',reverse(Formula))-1) Formula from #PhantomFormula where Id=@id
)
----将+、-替换为"+","-"
,cte_b as(
select ID,MeterNO, replace(replace(Formula,'+','"+"'),'-','"-"') Formula from cte_a
)
----将表达式split
,cte_c as(
select @id as id, * from dbo.f_split((select Formula from cte_b ),'"')
)
---按表达式数字查询 表“Meter”的数据,替换掉
,cte_d as(
select id,
case when col<>'+' and col<>'-'
then
(select cast(RtQty as nvarchar(100)) from #Meter where MeterNo=col)
else
col
end col
from cte_c
)

----查询最终结果
select t.id,MeterNO, replace(stuff((select '_'+cast (col as nvarchar(100)) from cte_d z where t.id=z.id for xml path('')), 1, 1, ''),'_','' ) as result
from cte_d t
join #PhantomFormula a on t.id=a.id
group by t.id ,a.MeterNO



drop table #PhantomFormula
drop table #Meter


查询结果

--------------------------------
id MeterNO result
3 XB0303 333.00+444.00-555.00

----------------------------------

(6 行受影响)

(4 行受影响)

(1 行受影响)



Viccy_Yao 2012-10-17
  • 打赏
  • 举报
回复
replace(063453,875.7)
replace(063454,8485.7)
max20120614 2012-10-17
  • 打赏
  • 举报
回复
我觉得是不是写一个自定义函数,功能返回编号对应的值,然后计算的时候直接调用函数,

如:XB0301=dbo.fuction(063457)+dbo.fuction(063470)
IEEE_China 2012-10-17
  • 打赏
  • 举报
回复
在SQL中用正则表达式替换

starryconfig 2012-10-17
  • 打赏
  • 举报
回复
怎么替换能写一下吗? 大师???

替换之后,结果就出来了啊。“875.7+8485.7”这样的,不就计算出来了吗?
快溜 2012-10-17
  • 打赏
  • 举报
回复
用Meter表中的RtQty替换Formula里的MeterNo,计算就麻烦了,你可以考虑读取到datatable里,datatable的computer方法可以计算字符串公式结果。
SQL77 2012-10-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
引用 1 楼 的回复:

sql里可以执行一个字符串变量,把变量内容当sql执行的
你这个只是还需要一个编号到数据的翻译



PhantomFormula表中的“063457+063470+063464-063473”中 的 “063457” 、“063470”这些,就是“Meter”表中的MeterNo,它们的值就是“RtQty”。 这些值要根据PhantomFormula表中的……
[/Quote]
提供思路。具体实现你可以自己试试

将公式 '063457+063470+063464-063473' 中的063457 replace 成另一个表的【RtQty】(值)
具体可以用字符匹配 LIKE 。CHARINDEX 等来关联REPLACE

替换后类似 '1+2+3-4'这样的RTQTY表达式。变成这样后可用CLR 或者 SQL EXEC OR FUNCTION 计算出结果。
starryconfig 2012-10-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

sql里可以执行一个字符串变量,把变量内容当sql执行的
你这个只是还需要一个编号到数据的翻译
[/Quote]


PhantomFormula表中的“063457+063470+063464-063473”中 的 “063457” 、“063470”这些,就是“Meter”表中的MeterNo,它们的值就是“RtQty”。 这些值要根据PhantomFormula表中的公式进行计算,这怎么翻译嘞???


能不能写点SQL, 大师。


starryconfig 2012-10-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

sql里可以执行一个字符串变量,把变量内容当sql执行的
你这个只是还需要一个编号到数据的翻译
[/Quote]

怎么翻译呢? 大师???

haitao 2012-10-17
  • 打赏
  • 举报
回复
sql里可以执行一个字符串变量,把变量内容当sql执行的
你这个只是还需要一个编号到数据的翻译

34,593

社区成员

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

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