Sum with inner join Oracle

0

People need help, I need to create a report that shows the total quantity of each product in stock, but I can not get the name of the products, only the product code and the sum of the quantity of the products I have. Here is the code I did:

SELECT 
    c.COD_PRODUTO,
    SUM(b.QTD_SALDO) AS SALDO 
FROM ESTOQUE_1 b
INNER JOIN ESTOQUE_2 c ON b.COD_PRODUTO = c.COD_PRODUTO
WHERE COD_PRODUTO IN ('6306','6308','6307','6234')
GROUP BY COD_PRODUTO;

In this code I have the following return:

SKU                            SALDO
------------------------- ----------
51231535                         210 
68000512                          24 
50388958                        1000 
50387203                        6000

I need the name of the item to appear, as shown below:

NOME  SKU                            SALDO
----- ------------------------- ----------
      51231535                         210 
      68000512                          24 
      50388958                        1000 
      50387203                        6000

The table that contains the product name is the STOCK_2

The table structure is:

ESTOQUE_1
COD_PRODUTO | QTD_SALDO

ESTOQUE_2
COD_PRODUTO | NOME_PRODUTO
    
asked by anonymous 30.06.2017 / 15:57

2 answers

0

You can put C.NAME_PRODUCT in select and in Group by. So the result will return the desired grouping

SELECT
    C.NOME_PRODUTO,
    c.COD_PRODUTO,
    SUM(b.QTD_SALDO) AS SALDO 
FROM ESTOQUE_1 b 
INNER JOIN ESTOQUE_2 c 
   ON b.COD_PRODUTO = c.COD_PRODUTO 
WHERE COD_PRODUTO IN ('6306','6308','6307','6234') 
GROUP BY COD_PRODUTO, C.NOME_PRODUTO;
    
30.06.2017 / 16:10
0

You just add the column of table c in Select and Group by

SELECT 
    c.NOME_PRODUTO,
    c.COD_PRODUTO,
    SUM(b.QTD_SALDO) AS SALDO 
FROM ESTOQUE_1 b
INNER JOIN ESTOQUE_2 c ON b.COD_PRODUTO = c.COD_PRODUTO
WHERE COD_PRODUTO IN ('6306','6308','6307','6234')
GROUP BY c.COD_PRODUTO,c.NOME_PRODUTO;
    
30.06.2017 / 16:01