Sub-consumption With Different Aggregate Results

Asked

Viewed 34 times

-1

Good afternoon, everyone,

I have two queries that work perfectly separate, but need to display in a grid information of the two queries.

I need to take all the inputs of a product and put in a column and in another column the outputs...

I made the so:

SELECT     CODIGO, DESCRICAO,
                          (SELECT     SUM(Produtos_Quant.QUANT) AS Expr1
                            FROM          Produtos_Quant INNER JOIN
                                                   produtos AS A ON Produtos_Quant.COD_PRODUTO = C.CODIGO
                            WHERE      (Produtos_Quant.TIPO <> 'REMOÇÃO')
                            GROUP BY Produtos_Quant.COD_PRODUTO) AS vEntradas,
                          (SELECT     SUM(pedidos_itens.QUANTIDADE) AS Expr1
                            FROM          pedidos_itens INNER JOIN
                                                   produtos AS B ON pedidos_itens.COD_PRODUTO = C.CODIGO INNER JOIN
                                                   pedidos ON pedidos_itens.COD_PEDIDO = pedidos.COD_PEDIDO
                            WHERE      (pedidos_itens.cancelado = 0) AND (pedidos.TIPO_PEDIDO <> 'ORÇAMENTO')
                            GROUP BY pedidos_itens.COD_PRODUTO) AS vSaidas
FROM         produtos AS C

Make no mistake... but the result goes wrong.

Example:

Making the separate inquiry the REPELEX product has 13 inputs and 12 outputs

Now making that way the same product has 76608 inputs and 82992 outputs

I need to place a product in each line with its inputs and outputs (sum).

Where am I going wrong?

  • put in the question the structure of the tables and some sample data to help realize better what is wrong

  • take the groupy by of the queries , having the model sure can improve the query

2 answers

0

I think what you want is:

SELECT     CODIGO, DESCRICAO,
           (SELECT     SUM(Produtos_Quant.QUANT) AS Expr1
            FROM      Produtos_Quant
            WHERE     (Produtos_Quant.COD_PRODUTO = C.CODIGO) AND 
                      (Produtos_Quant.TIPO <> 'REMOÇÃO')
           ) AS vEntradas,
           (SELECT     SUM(pedidos_itens.QUANTIDADE) AS Expr1
            FROM      pedidos_itens INNER JOIN pedidos ON pedidos_itens.COD_PEDIDO = pedidos.COD_PEDIDO
            WHERE     (pedidos_itens.cancelado = 0) AND 
                      (pedidos.TIPO_PEDIDO <> 'ORÇAMENTO') AND 
                      (pedidos_itens.COD_PRODUTO = C.CODIGO)
           ) AS vSaidas
FROM       produtos AS C

correlate the most external select product code with the product in each subselect.

0

My friend, you made a mistake in your code... I believe it’s because of the lack of the first subselect (inputs) Inner Join... I modified it and it was like this:

SELECT     CODIGO, DESCRICAO,
                          (SELECT     SUM(Produtos_Quant.QUANT) AS Expr1
                            FROM          Produtos_Quant INNER JOIN
                                                   produtos ON Produtos_Quant.COD_PRODUTO = C.CODIGO
                            WHERE      (Produtos_Quant.COD_PRODUTO = C.CODIGO) AND (Produtos_Quant.TIPO <> 'REMOÇÃO')) AS vEntradas,
                          (SELECT     SUM(pedidos_itens.QUANTIDADE) AS Expr1
                            FROM          pedidos_itens INNER JOIN
                                                   pedidos ON pedidos_itens.COD_PEDIDO = pedidos.COD_PEDIDO
                            WHERE      (pedidos_itens.cancelado = 0) AND (pedidos.TIPO_PEDIDO <> 'ORÇAMENTO') AND (pedidos_itens.COD_PRODUTO = C.CODIGO)) AS vSaidas
FROM         produtos AS C

However, the result of the first subselect was a wrong value (vEntradas), plus the second subselect (vSaidas) gave right.

Where is the error?

  • You have to reference the most external select (C) products table and not make a Join Inner. The second subselect worked because you don’t make a new Ner Join with the products table. Really had an error in my answer I ended up copying, when I thought I was moving, the condition (Produtos_Quant.COD_PRODUTO = C.CODIGO). Corrected.

  • this part will stay that way (FROM Products_quant Products_quant.COD_PRODUTO = C.CODE) ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.