通过将两个表连接在一起,可以将总和的结果重复

时间:2019-06-06 13:28:04

标签: mysql database duplicates mysql-workbench

我有两个表,一个表显示我从产品购买的单位和从产品中接收的单位,在第二个表中,每个商店的产品价格,每个商店的产品价格都不同。我需要提供购买的总价值和收到的总价值以及购买的商品和收到的商品之间的差额。

因此每个商店都有自己的价格,使摘要将所有值加起来,结果是非常大的。

当前查询:

Select NROOC,sum(cant_oc), sum(CANT_REC), 
        sum(cant_oc)-sum(CANT_REC) as DIF,
        (sum(cant_oc*costo_PPF))-(sum(CANT_REC*costo_PPF)) as DIF_costo
from oc_contenedores_odbms 
    left join costo_PPF on oc_contenedores_odbms.SKU=costo_PPF.sku
where NROOC in ('9053963');

名称表1:costo_PPF

 +-------------+----------+------------+----------------------+--+
 | #           |  sku     |  costo_PPF |  fecha_actualizacion |  |
 |   id_tienda |          |            |                      |  |
 +-------------+----------+------------+----------------------+--+
 | 2           | 2618796  | 6710,38    |  2019-06-05          |  |
 +-------------+----------+------------+----------------------+--+
 | 2           | 2669161  | 6076,25    |  2019-06-05          |  |
 +-------------+----------+------------+----------------------+--+
 | 2           |  266917X | 6292,23    |  2019-06-05          |  |
 +-------------+----------+------------+----------------------+--+
 | 2           | 2669188  | 6289,89    |  2019-06-05          |  |
 +-------------+----------+------------+----------------------+--+
 | 2           |  266920X | 5693,62    |  2019-06-05          |  |
 +-------------+----------+------------+----------------------+--+
 | 2           | 2742322  | 6399,75    |  2019-06-05          |  |
 +-------------+----------+------------+----------------------+--+
 | 3           | 2669161  | 6066,39    |  2019-06-05          |  |
 +-------------+----------+------------+----------------------+--+
 | 3           |  266917X | 5981,14    |  2019-06-05          |  |
 +-------------+----------+------------+----------------------+--+
 | 3           | 2669188  | 6496,81    |  2019-06-05          |  |
 +-------------+----------+------------+----------------------+--+
 | 3           |  266920X | 5723,14    |  2019-06-05          |  |
 +-------------+----------+------------+----------------------+--+
 | 3           | 2742322  | 6364,17    |  2019-06-05          |  |
 +-------------+----------+------------+----------------------+--+
 | 3           | 2618796  | 6710,38    |  2019-06-05          |  |
 +-------------+----------+------------+----------------------+--+

名称表2:oc_contenedores_odbms

 +----------+---------+----------+-----------+--------+------+
 |  SKU     |  NROOC  |  CANT_OC |  CANT_REC |  LOCAL |  DIF |
 +----------+---------+----------+-----------+--------+------+
 | 2618796  | 9053963 | 744      | 744       | 508    | 0    |
 +----------+---------+----------+-----------+--------+------+
 | 2669161  | 9053963 | 2232     | 0         | 508    | 2232 |
 +----------+---------+----------+-----------+--------+------+
 |  266917X | 9053963 | 3720     | 744       | 508    | 2976 |
 +----------+---------+----------+-----------+--------+------+
 | 2669188  | 9053963 | 2232     | 1488      | 508    | 744  |
 +----------+---------+----------+-----------+--------+------+
 |  266920X | 9053963 | 1728     | 0         | 508    | 1728 |
 +----------+---------+----------+-----------+--------+------+
 | 2742322  | 9053963 | 968      | 0         | 508    | 968  |
     +----------+---------+----------+-----------+--------+------+

实际结果:

+---------+---------------+----------------+-------+-------------+
| NROOC   | Suma(CANT_OC) | Suma(CANT_REC) | DIF   | DIF_costo   |
+---------+---------------+----------------+-------+-------------+
| 9053963 | 23248         | 5952           | 17296 |105.224.862,2|
+---------+---------------+----------------+-------+-------------+

预期结果:

+---------+---------------+----------------+------+---------------+
| NROOC   | Suma(CANT_OC) | Suma(CANT_REC) | DIF  | DIF_costo     |
+---------+---------------+----------------+------+---------------+
| 9053963 | 11624         | 2976           | 8648 | 52.126.284,72 |
+---------+---------------+----------------+------+---------------+

0 个答案:

没有答案