Sql Server 指定列的乘积

需要统计指定列的乘积
SQL SERVER中有EXP(SUM(LOG(字段)))的函数
如下例子:

SELECT    WorkOrder ,
          LineId ,
          ROUND(EXP(SUM(LOG(ThroughRate))), 4) * 100 AS ThroughRate
          FROM   #ThroughRate
          GROUP BY  WorkOrder ,
                    LineId

EXP(SUM(LOG(字段))) 字段的值是不能够包含0的,否则出现无效浮点数操作的错误
其实如果指定列含有0的话,最终的乘积是0;所以考虑把结果集分成两部分,等于0以及不等于0两种情况计算,最终在合并查询处理

 SELECT  m.WorkOrder ,
                m.LineId ,
                CASE WHEN n.ThroughRate = 0 THEN 0
                     ELSE CONVERT(DECIMAL(18, 4), m.ThroughRate)
                END AS ThroughRate
        FROM    ( SELECT    WorkOrder ,
                            LineId ,
                            ROUND(EXP(SUM(LOG(ThroughRate))), 4) * 100 AS ThroughRate
                  FROM      #ThroughRate
                  WHERE     ThroughRate != 0
                  GROUP BY  WorkOrder ,
                            LineId
                ) m
                LEFT JOIN ( SELECT  WorkOrder ,
                                    LineId ,
                                    ThroughRate
                            FROM    #ThroughRate
                            WHERE   ThroughRate = 0
                            GROUP BY WorkOrder ,
                                    LineId ,
                                    ThroughRate
                          ) n ON m.WorkOrder = n.WorkOrder
                                 AND m.LineId = n.LineId 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值