Skip to main content
traduce título al español
Enlace
ffflabs
  • 23k
  • 28
  • 50

MySQL SUM same id in different columns() agrupando por un mismo ID en diferentes columnas

se añadieron 159 caracteres en el cuerpo
Origen Enlace

the problem is thisEste es el problema a resolver:

I have a table where I registerTengo una tabla, en la cual se registran desde 1 hasta 5 different productsdiferentes productos, a product can be both in the fieldpero no hay un control de orden al registrar los productos, en un registro un producto puede estar en el campo product_1 in they en otro registro puede estar en el campo product_5, what I need is to add the quantity of each product and group it by id.esta es la estructura de la tabla:

This is the structure of the table. *-----------------++ |ID | |DATE | |ID_PRODUCT_1 | |QUANTITY_PRODUCT_1| |ID_PRODUCT_2 | |QUANTITY_PRODUCT_2| |ID_PRODUCT_3 | |QUANTITY_PRODUCT_3| |ID_PRODUCT_4 | |QUANTITY_PRODUCT_4| |ID_PRODUCT_5 | |QUANTITY_PRODUCT_5| +------------------+

    *-----------------++
    |ID                |
    |DATE              |
    |ID_PRODUCT_1      |
    |QUANTITY_PRODUCT_1|
    |ID_PRODUCT_2      |
    |QUANTITY_PRODUCT_2|
    |ID_PRODUCT_3      |
    |QUANTITY_PRODUCT_3|
    |ID_PRODUCT_4      |
    |QUANTITY_PRODUCT_4|
    |ID_PRODUCT_5      |
    |QUANTITY_PRODUCT_5|
    +------------------+

And these are some example records, what I need is to add the quantity of the product with id = 1001, although this id exists in the other records, but in a different fieldEstos son unos registros de ejemplo, and also sum the other products respectively.de lo que acabo de explicar:

so that in the end this Is the resultLo que necesito es sumar la cantidad de cada producto, sin importar en qué campo haya sido registrado, y al final este sería el resultado:

    +--------------------------------+
    |SUM(PRODUCT WITH ID 1001) = 2600|
    |SUM(PRODUCT WITH ID 1002) = 2000|
    |SUM(PRODUCT WITH ID 1003) = 2000|
    +--------------------------------+

SUM(PRODUCT WITH ID 1001) = 2600 SUM(PRODUCT WITH ID 1002) = 2000 SUM(PRODUCT WITH ID 1003) = 2000Muchas gracias.

the problem is this:

I have a table where I register 5 different products, a product can be both in the field product_1 in the product_5, what I need is to add the quantity of each product and group it by id.

This is the structure of the table. *-----------------++ |ID | |DATE | |ID_PRODUCT_1 | |QUANTITY_PRODUCT_1| |ID_PRODUCT_2 | |QUANTITY_PRODUCT_2| |ID_PRODUCT_3 | |QUANTITY_PRODUCT_3| |ID_PRODUCT_4 | |QUANTITY_PRODUCT_4| |ID_PRODUCT_5 | |QUANTITY_PRODUCT_5| +------------------+

And these are some example records, what I need is to add the quantity of the product with id = 1001, although this id exists in the other records, but in a different field, and also sum the other products respectively.

so that in the end this Is the result:

SUM(PRODUCT WITH ID 1001) = 2600 SUM(PRODUCT WITH ID 1002) = 2000 SUM(PRODUCT WITH ID 1003) = 2000

Este es el problema a resolver:

Tengo una tabla, en la cual se registran desde 1 hasta 5 diferentes productos, pero no hay un control de orden al registrar los productos, en un registro un producto puede estar en el campo product_1 y en otro registro puede estar en el campo product_5, esta es la estructura de la tabla:

    *-----------------++
    |ID                |
    |DATE              |
    |ID_PRODUCT_1      |
    |QUANTITY_PRODUCT_1|
    |ID_PRODUCT_2      |
    |QUANTITY_PRODUCT_2|
    |ID_PRODUCT_3      |
    |QUANTITY_PRODUCT_3|
    |ID_PRODUCT_4      |
    |QUANTITY_PRODUCT_4|
    |ID_PRODUCT_5      |
    |QUANTITY_PRODUCT_5|
    +------------------+

Estos son unos registros de ejemplo, de lo que acabo de explicar:

Lo que necesito es sumar la cantidad de cada producto, sin importar en qué campo haya sido registrado, y al final este sería el resultado:

    +--------------------------------+
    |SUM(PRODUCT WITH ID 1001) = 2600|
    |SUM(PRODUCT WITH ID 1002) = 2000|
    |SUM(PRODUCT WITH ID 1003) = 2000|
    +--------------------------------+

Muchas gracias.

Origen Enlace

MySQL SUM same id in different columns

the problem is this:

I have a table where I register 5 different products, a product can be both in the field product_1 in the product_5, what I need is to add the quantity of each product and group it by id.

This is the structure of the table. *-----------------++ |ID | |DATE | |ID_PRODUCT_1 | |QUANTITY_PRODUCT_1| |ID_PRODUCT_2 | |QUANTITY_PRODUCT_2| |ID_PRODUCT_3 | |QUANTITY_PRODUCT_3| |ID_PRODUCT_4 | |QUANTITY_PRODUCT_4| |ID_PRODUCT_5 | |QUANTITY_PRODUCT_5| +------------------+

And these are some example records, what I need is to add the quantity of the product with id = 1001, although this id exists in the other records, but in a different field, and also sum the other products respectively.

    *--------------------------------+
    |ID                 = 1          |
    |DATE               = 2017-02-03 |
    |ID_PRODUCT_1       = 1001       |
    |QUANTITY_PRODUCT_1 = 1400       |
    |ID_PRODUCT_2       = 1002       |
    |QUANTITY_PRODUCT_2 = 600        |
    |ID_PRODUCT_3       = -          |
    |QUANTITY_PRODUCT_3 = -          |
    |ID_PRODUCT_4       = -          |
    |QUANTITY_PRODUCT_4 = -          |
    |ID_PRODUCT_5       = -          |
    |QUANTITY_PRODUCT_5 = -          |
    +--------------------------------+

    *--------------------------------+
    |ID                 = 2          |
    |DATE               = 2017-02-03 |
    |ID_PRODUCT_1       = 1003       |
    |QUANTITY_PRODUCT_1 = 1400       |
    |ID_PRODUCT_2       = 1001       |
    |QUANTITY_PRODUCT_2 = 1000       |
    |ID_PRODUCT_3       = -          |
    |QUANTITY_PRODUCT_3 = -          |
    |ID_PRODUCT_4       = -          |
    |QUANTITY_PRODUCT_4 = -          |
    |ID_PRODUCT_5       = -          |
    |QUANTITY_PRODUCT_5 = -          |
    +--------------------------------+
    *--------------------------------+
    |ID                 = 3          |
    |DATE               = 2017-02-03 |
    |ID_PRODUCT_1       = 1002       |
    |QUANTITY_PRODUCT_1 = 1400       |
    |ID_PRODUCT_2       = 1003       |
    |QUANTITY_PRODUCT_2 = 600        |
    |ID_PRODUCT_3       = 1001       |
    |QUANTITY_PRODUCT_3 = 200        |
    |ID_PRODUCT_4       = -          |
    |QUANTITY_PRODUCT_4 = -          |
    |ID_PRODUCT_5       = -          |
    |QUANTITY_PRODUCT_5 = -          |
    +--------------------------------+

so that in the end this Is the result:

SUM(PRODUCT WITH ID 1001) = 2600 SUM(PRODUCT WITH ID 1002) = 2000 SUM(PRODUCT WITH ID 1003) = 2000