首页 文章

SUM中的前一个值

提问于
浏览
1

如何创建一个能够使用新行的前一行 SUM 并显示每行结果的函数?

在“SALDO”栏中这样:

| Cantidad           | nombre del producto | Precio compra | Precio DNM | Precio venta | Total compra | Saldo   | 
+--------------------+---------------------+---------------+------------+--------------+--------------+---------+
|                 14 | Sargenorig          |          6.00 |       9.00 |        10.00 |           84 |      84 | 
|                  9 | nombre producto     |        100.00 |     100.00 |       120.00 |          900 |     984 | 
| Total de productos |                     |               |            |              | Total        | Reporte | 
| 23                 |                     |               |            |              |              |     984 |

tExiste 是产品数量的总和,这是所有存在的总和减去每个产品的所有销售总和

Tott 是总购买量的总和,这是所有存在的总和减去每个产品的所有销售总和,结果乘以每个产品的购买价格

$sql = "SELECT nombreProd, compra, venta, dnm, 
SUM(existencia - vendido) AS tExiste, 
 (SUM(existencia - vendido) * compra) AS Tott
FROM PRODUCTOS WHERE f_producto BETWEEN 
'".$fromdate."' AND '".$todate."' GROUP BY code 
ORDER BY id DESC";
$result = $conn->query($sql);
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
    echo '
        <tbody>
            <tr>
                <td>'.$row["tExiste"].'</td>
                <td class="center">'.$row["nombreProd"].'</td>
                <td class="center">
                <span class="add-on">'.$moneda.' </span>'.$row["compra"].'
                </td>
                <td class="center">
                <span class="add-on">'.$moneda.' </span>'.$row["dnm"].'
                </td>
                <td class="center">
                <span class="add-on">'.$moneda.' </span>'.$row["venta"].'
                </td>
                <td>
                <span class="add-on">'.$moneda.' </span>'.$row["Tott"].'
                </td>    
                <td>
                <span class="add-on">'.$moneda.' </span>
                </td>  // Here is where I need show the sum of each previous row with the new one              
            </tr>                         
        </tbody>
    '; }

最后我需要显示那些 SUM 的总数量


编辑

我需要将之前的托特加到下一个托特(Tott Tott),而不是总和的存在总和(tExiste Tott)......

例如:R1:84 R2:900 = 984 R3:150 = 1134 ......等

谢谢

2 回答

  • 2

    如果我是你,我只是在PHP中用你需要求和的所有值做一个简单的总和 echo 那个(我没跟随你需要求和的所以我给你举个例子) . 我总是建议,如果可能的话,你应该让PHP完成工作,而不是你的数据库 .

    $prev = 0;
    while($row = $result->fetch(PDO::FETCH_ASSOC)) {
    echo '
        <tbody>
            <tr>
                <td>'.$row["tExiste"].'</td>
                <td class="center">'.$row["nombreProd"].'</td>
                <td class="center">
                <span class="add-on">'.$moneda.' </span>'.$row["compra"].'
                </td>
                <td class="center">
                <span class="add-on">'.$moneda.' </span>'.$row["dnm"].'
                </td>
                <td class="center">
                <span class="add-on">'.$moneda.' </span>'.$row["venta"].'
                </td>
                <td>
                <span class="add-on">'.$moneda.' </span>'.$row["Tott"].'
                </td>    
                <td>
                <span class="add-on">'.$moneda.' </span>
                </td>
                <td>' . ($row["Tott"] + $prev) . '</td>   
            </tr>                         
        </tbody>
    ';
    $prev = $row["Tott"];
    }
    
  • 0

    您可以使用WITH ROLLUP通过SQL获取总计 .

    从文档

    SELECT IFNULL(year,"Total") as year, SUM(profit) FROM sales GROUP BY year WITH ROLLUP;
    
    
    +-------+-------------+
    | year  | SUM(profit) |
    +-------+-------------+
    |  2000 |        4525 |
    |  2001 |        3010 |
    | Total |        7535 |
    +-------+-------------+
    

相关问题