求乘积

lnming 2010-08-02 09:27:56
Series Inv ABC
1 1 26
1 1 15
1 1 37
1 1 26
1 1 32
1 1 23
1 2 29
1 2 20
1 2 23
1 2 29
1 2 23
1 2 28
1 3 27
1 3 26
1 3 26
1 3 27
1 3 26
1 3 25
1 4 30
1 4 26
1 4 29
1 4 27
1 4 24
1 4 29
1 5 21
1 5 15
1 5 29
1 5 34
1 5 29
1 5 20
这是数据库的值
SELECT 974as Series,1 as Inv,
POWER(10.00000000,SUM(LOG10([01])))/100000000 AS '01',
POWER(10.00000000,SUM(LOG10([02])))/100000000 AS '02',
POWER(10.00000000,SUM(LOG10([03])))/100000000 AS '03',
POWER(10.00000000,SUM(LOG10([04])))/100000000 AS '04',
POWER(10.00000000,SUM(LOG10([05])))/100000000 AS '05',
POWER(10.00000000,SUM(LOG10([06])))/100000000 AS '06',
POWER(10.00000000,SUM(LOG10([07])))/100000000 AS '07',
POWER(10.00000000,SUM(LOG10([08])))/100000000 AS '08',
POWER(10.00000000,SUM(LOG10([09])))/100000000 AS '09',
POWER(10.00000000,SUM(LOG10([10])))/100000000 AS '10',
POWER(10.00000000,SUM(LOG10([11])))/100000000 AS '11',
POWER(10.00000000,SUM(LOG10([12])))/100000000 AS '12',
POWER(10.00000000,SUM(LOG10([13])))/100000000 AS '13',
POWER(10.00000000,SUM(LOG10([14])))/100000000 AS '14',
POWER(10.00000000,SUM(LOG10([15])))/100000000 AS '15',
POWER(10.00000000,SUM(LOG10([16])))/100000000 AS '16',
POWER(10.00000000,SUM(LOG10([17])))/100000000 AS '17',
POWER(10.00000000,SUM(LOG10([18])))/100000000 AS '18',
POWER(10.00000000,SUM(LOG10([19])))/100000000 AS '19',
POWER(10.00000000,SUM(LOG10([20])))/100000000 AS '20',
POWER(10.00000000,SUM(LOG10([21])))/100000000 AS '21',
POWER(10.00000000,SUM(LOG10([22])))/100000000 AS '22',
POWER(10.00000000,SUM(LOG10([23])))/100000000 AS '23',
POWER(10.00000000,SUM(LOG10([24])))/100000000 AS '24',
POWER(10.00000000,SUM(LOG10([25])))/100000000 AS '25',
POWER(10.00000000,SUM(LOG10([26])))/100000000 AS '26',
POWER(10.00000000,SUM(LOG10([27])))/100000000 AS '27',
POWER(10.00000000,SUM(LOG10([28])))/100000000 AS '28',
POWER(10.00000000,SUM(LOG10([29])))/100000000 AS '29',
POWER(10.00000000,SUM(LOG10([30])))/100000000 AS '30',
POWER(10.00000000,SUM(LOG10([31])))/100000000 AS '31',
POWER(10.00000000,SUM(LOG10([32])))/100000000 AS '32',
POWER(10.00000000,SUM(LOG10([33])))/100000000 AS '33'
FROM TRedFollowInvGroup WHERE Series = 974 and Inv = 1
这是我的SQL语句
截了一部分的数据,要求的是某一Series的ABC字段的连乘。但是直接连乘的话,乘积过大,无法正常显示,所以采用一个INV值连乘一次,然后再除到一两位数,再连乘每个IVN得出的乘积。但感觉自己的SQL语句不过关,因为这样的话,要多次调用SQL语句,多少个INV值就是调用多少次,然后又要再连乘,不知道有没有一个SQL语句就搞定了的。最后是显示除了N倍后的数值。
...全文
135 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hokor 2010-08-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lnming 的回复:]
我的数据实际每个序号有198行,我取这个乘积是用于比较,所以可以除了10的N倍,只要正常显示,小数点长点都没关系。
[/Quote]不是10的N倍式10的N次方。10的200次方大概等于2的600到800次方之间吧。。这个数不好计算机不好处理啊,比天上的星星还多了吧,把全世界人的手指头加脚趾头都用上也不够使啊|!!
SQLCenter 2010-08-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lnming 的回复:]

我的数据实际每个序号有198行,我取这个乘积是用于比较,所以可以除了10的N倍,只要正常显示,小数点长点都没关系。
[/Quote]

那这样就行了:

exp(sum(log(abc/10.0)))
POWER(10.00000000,SUM(LOG10(ABC/10.0)))
lnming 2010-08-02
  • 打赏
  • 举报
回复
我的数据实际每个序号有198行,我取这个乘积是用于比较,所以可以除了10的N倍,只要正常显示,小数点长点都没关系。
SQLCenter 2010-08-02
  • 打赏
  • 举报
回复
--> 测试数据:#
if object_id('tempdb.dbo.#') is not null drop table #
create table #(Series int, Inv int, ABC int)
insert into #
select 1, 1, 26 union all
select 1, 1, 15 union all
select 1, 1, 37 union all
select 1, 1, 26 union all
select 1, 1, 32 union all
select 1, 1, 23 union all
select 1, 2, 29 union all
select 1, 2, 20 union all
select 1, 2, 23 union all
select 1, 2, 29 union all
select 1, 2, 23 union all
select 1, 2, 28 union all
select 1, 3, 27 union all
select 1, 3, 26 union all
select 1, 3, 26 union all
select 1, 3, 27 union all
select 1, 3, 26 union all
select 1, 3, 25 union all
select 1, 4, 30 union all
select 1, 4, 26 union all
select 1, 4, 29 union all
select 1, 4, 27 union all
select 1, 4, 24 union all
select 1, 4, 29 union all
select 1, 5, 21 union all
select 1, 5, 15 union all
select 1, 5, 29 union all
select 1, 5, 34 union all
select 1, 5, 29 union all
select 1, 5, 20


select Series, exp(sum(log(abc)))abc from # group by Series
/*
Series abc
----------- ----------------------
1 1.68743027138102E+42
*/

select Series, POWER(cast(10 as float),SUM(LOG10(ABC)))abc from # group by Series
/*
Series abc
----------- ----------------------
1 1.68743027138102E+42
*/


如果超出float的范围,就无法计算了。

POWER(10.00000000... 按numeric类型计算。
黄_瓜 2010-08-02
  • 打赏
  • 举报
回复
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([Series] int,[Inv] int,[ABC] int)
insert [tb]
select 1,1,26 union all
select 1,1,15 union all
select 1,1,37 union all
select 1,1,26 union all
select 1,1,32 union all
select 1,1,23 union all
select 1,2,29 union all
select 1,2,20 union all
select 1,2,23 union all
select 1,2,29 union all
select 1,2,23 union all
select 1,2,28 union all
select 1,3,27 union all
select 1,3,26 union all
select 1,3,26

declare @i decimal(38,0)
--38位不短了吧?
set @i=1
select @i=@i*[ABC] from [tb]
select @i
/*
----------------------------------------
1255647245683280486400

(所影响的行数为 1 行)

*/

27,582

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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