如何在Sql中乘以列

时间:2016-06-15 07:53:38

标签: sql-server

我有一栏:

-0.75640390000 --number1
0.58303570000  --number2
-0.07794400000 --number3
0.00000000000  -- ....
0.58303570000
-0.07794400000
0.42976550000
0.58781540000
0.00909080000
0.58781540000

如何计算(1+number1/100)*(1+number2/100)*(1+number3/100)*... etc

1 个答案:

答案 0 :(得分:0)

如果def draw_gen_checkers(img, s, colors): n = 0 for i in range(len(img)/s + 1): for j in range(len(img[0])/s + 1): for ii in range(i*s, i*s+s): for jj in range(j*s,j*s+s): if 0 <= ii < len(img) and 0 <= jj < len(img[0]): img[ii][jj] = colors[n] n = (n+1) % len(colors) im.visd(img) img = im.create(300, 200, (0,0,0)) colors = [(255,128,0),(0,0,255),(0,255,0),(255,0,0)] s = 25 是第一行的值而number1是下一行的值,则使用number2

ROW_NUMBER()

输出:

;WITH cte AS (
SELECT  col,
        ROW_NUMBER() OVER (ORDER BY (SELECT 1)) as rn
FROM (VALUES
(-0.75640390000),
(0.58303570000),
(-0.07794400000),
(0.00000000000),
(0.58303570000),
(-0.07794400000),
(0.42976550000),
(0.58781540000),
(0.00909080000),
(0.58781540000)
) as t(col)
), rec AS (
SELECT  CAST((1+col/100) as float) as d,
        1 as l
FROM cte
WHERE rn = 1
UNION ALL
SELECT  d * (1+col/100),
        l+1
FROM rec r
INNER JOIN cte c
    ON c.rn = l+1
)
SELECT TOP 1 d
FROM rec
ORDER BY l DESC

<强> EDIT1

接受了200万行的挑战:)我创建了表:

1,01874941736518

把它放进去:

CREATE TABLE [dbo].[Testing](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [col] [decimal](19, 11) NULL,
CONSTRAINT [PK_id_test] PRIMARY KEY CLUSTERED ([id] ASC)
) 

然后做到这一点:

;WITH cte AS (
SELECT  CAST(0.00000000002 as decimal(19,11)) as col, 
        1 as rn
UNION ALL 
SELECT  CAST(col + 0.00000000002 as decimal(19,11)),
        rn+1
FROM cte 
WHERE rn < 2000000
)

INSERT INTO [dbo].[Testing] ([col])
SELECT col from cte
OPTION (MAXRECURSION 0)

输出是:

;WITH rec AS (
SELECT  CAST((1+col/100) as decimal(19,11)) as d,
        1 as l
FROM [dbo].[Testing]
WHERE id = 1
UNION ALL
SELECT CAST( d * (1+col/100) as decimal(19,11)),
        l+1
FROM rec r
INNER JOIN [dbo].[Testing] c
    ON c.id = l+1
)

SELECT top 1 d
FROM rec
ORDER BY l desc
OPTION (MAXRECURSION 0)

我的本​​地电脑在16秒内执行了查询。

<强> EDIT2

在临时表的帮助下:

1.49182491770