将Excel公式转换为T-SQL

时间:2017-10-19 20:21:32

标签: sql-server excel tsql excel-formula

我有一个Excel公式,我试图转换为SQL。公式如下

=1800*85%/450*E23

假设E23中的值为12,那么公式将等于40.8

在SQL中我写了这个:

DECLARE @Number1 INT
DECLARE @Number2 INT
Declare @Number3 INT
Declare @Number4 FLOAT 


Set @Number1 = 1800
Set @Number2 = 85
Set @Number3 = 450

SELECT @Number1 * @Number2 / @Number3 * Column1 AS FormulaResult 
FROM DB;

如果有人可以建议如何将@ Number2 = 85变为85%,那么我可以得到与Excel相同的结果我会非常感激它。

谢谢

1 个答案:

答案 0 :(得分:3)

这是简单的数学运算。注意:在85%中将85转为0.01。

DECLARE 
  @Number1 INT,
  @Number2 INT,
  @Number3 INT;
--Declare @Number4 FLOAT; -- don't know what you're doing with this

Set @Number1 = 1800
Set @Number2 = 85
Set @Number3 = 450

DECLARE @DB table (column1 int);
INSERT @DB VALUES (10),(11),(12),(15),(20);

SELECT column1, @Number1 * (@Number2*.01) / @Number3 * column1 AS FormulaResult 
FROM @DB;

<强>结果

column1     FormulaResult
----------- ---------------------------------------
10          34.000000
11          37.400000
12          40.800000
15          51.000000
20          68.000000