在SQL中运行乘法

时间:2016-07-13 23:25:59

标签: sql ms-access

我想在MS Access数据库中使用SQL查询执行运行乘法。

 SELECT Table.ITEM
,Table.Location
,Table.PATH
,Table.PARENT
,Table.CATEGORY
,Table.origin
,Table.qty
,Table.rate
,(Produced * Rate) AS EFF_Rate
,(
    SELECT max(QTY) AS ProducedQty
    FROM TableAS B
    GROUP BY ITEM
    ) AS Produced
    FROM Table;

我将此作为示例输出。(查询Result.jpeg)enter image description here  想要一个能够让我运行乘法的列。例如,根据查询数据集的结果:

例如,如果我乘以Max(数量),即乘以5026,它将给我4931.0086。现在我想将4931.0086乘以1(即费率字段的第二行值),依此类推。附加所需的输出Output

1 个答案:

答案 0 :(得分:0)

我创建了一张经过测试的表,但它确实有效:

    SELECT rate,eff_rate,
        (
         case @RunMult when 0 then @RunMult := eff_rate
         else @RunMult := @RunMult * rate end
        ) as RunningMultipl
    FROM rate,
       (SELECT @RunMult:=0) t;

结果:

    +--------+-----------+----------------+
    | rate   | eff_rate  | RunningMultipl |
    +--------+-----------+----------------+
    | 0.9811 | 4931.0086 |      4931.0086 |
    |      1 |      5026 |      4931.0086 |
    | 0.9278 | 4663.1228 |  4574.98977908 |
    +--------+-----------+----------------+

要在查询中按预期工作,需要进行一些更改:

    SELECT t1.*, 
        (
         case @RunMult when 0 then @RunMult := t1.eff_rate
         else @RunMult := @RunMult * t1.rate end
        ) as RunningMultipl
    FROM
    (
        SELECT Table.ITEM
        ,Table.Location
        ,Table.PATH
        ,Table.PARENT
        ,Table.CATEGORY
        ,Table.origin
        ,Table.qty
        ,Table.rate
        ,(Produced * Rate) AS EFF_Rate
        ,(
            SELECT max(QTY) AS ProducedQty
            FROM TableAS B
            GROUP BY ITEM
            ) AS Produced
            FROM Table
        )as t1, 
        (SELECT @RunMult:=0) t2;