1

十进制类型字段需要拆分为范围(区间),以不同方式计算,然后求和。

这个问题涉及字段值,而不是行。因此,对于表中每一行中的某个字段(fieldX):

FieldX <  0.013           <----remain just FieldX
FieldX >= 0.013 to 0.026  <----be multiplied with 50%.
FieldX >  0.026           <----be omitted.

编辑评论:[0.013, 0.026]下面的示例显示了表达式为封闭封闭范围内的值:

  • 0.013 + (FieldX - 0.013) * 0.5

然后这些范围(间隔)或部分需要相加。

示例案例:

FieldX     Result
-0.12      -0.12
-0.05      -0.05
+0.05      +0.05                                 — mismatch with specification
+0.011     +0.011
+0.014     +0.0135 = (0.013 + (0.014-0.013)*50%)
+0.021     +0.017  = (0.013 + (0.021-0.013)*50%)
+0.026     +0.0195 = (0.013 + (0.026-0.013)*50%)
+0.031     +0.0195 = (0.013 + (0.026-0.013)*50%) — mismatch with specification

我将颁发诺贝尔奖给解决它的天才!

4

2 回答 2

0

您应该能够使用CASE 语句执行此操作

SELECT  *,
        CASE 
            WHEN FieldX < 0.013
                THEN FieldX
            WHEN FieldX >= 0.013 and FieldX < 0.026 
                THEN 0.013 + (FieldX - 0.013 ) * .5
            ... and so on
        END
FROM    [Table]
于 2012-09-18T04:07:20.273 回答
0
SELECT SUM(CASE
           WHEN FieldX <  0.013 THEN FieldX
           WHEN FieldX <= 0.026 THEN 0.013 + 0.5 * (FieldX - 0.013)
           ELSE                     NULL
           END)
  FROM YourTable

聚合忽略 NULL,相当于忽略它。请注意,对于 SUM(),您可以使用 0.0 代替 NULL,但不能用于 COUNT 或 AVG。忽略行的另一种方法是使用 WHERE 子句将它们过滤掉,当然:

SELECT SUM(CASE
           WHEN FieldX <  0.013 THEN FieldX
           WHEN FieldX <= 0.026 THEN 0.013 + 0.5 * (FieldX - 0.013)
           END)
  FROM YourTable
 WHERE FieldX <= 0.026
于 2012-09-18T05:56:06.757 回答