Add a column where its values are already the result of another sum

Asked

Viewed 50 times

0

I have 2 tables in a database, one contains the purchase orders, another contains the items of these purchase orders, this data is obtained from an import from another system so I do not have foreign key conveying the tables, but I have information that I can uni-las no select.

Purchase Orders: inserir a descrição da imagem aqui

Items of Purchase orders: inserir a descrição da imagem aqui Note: These are just some of the records I have in the database to use as an example.

The select I make is this:

SELECT sc.empresa, sc.cod_autorizante, sc.codigo_ordem_compra, ( 
    SELECT SUM(ioc.valor_total) as soma 
    FROM itens_ordens_compras ioc 
    WHERE ioc.codigo_empresa = sc.empresa 
    and ioc.cod_ordem_compra = sc.codigo_ordem_compra ) as valor_t 
from solicitacao_compras sc 
where sc.cod_autorizante = 222 
and sc.data_aprovacao like '%/10/2020' 
GROUP by sc.codigo_ordem_compra;

Note. 2: I do with the GROUP by sc.codigo_ordem_purchase because without it I have records that end up repeating themselves.

And I can get this result:

inserir a descrição da imagem aqui

My question and need is, I can give a select next to this only by adding a column valor_t where would bring me in the case of this example the value R$203,034.66, the other information would not need to appear, only this final value same.

From now on, thank you.

1 answer

1


Good afternoon,

I have no experience with Mysql, but if the version you are using allows you to use CTE (Common Table Expressions), you can try it in a similar way to the one below:

with CTE_Origem as
(
    -- consulta inicial
)

select sum(soma) as soma from CTE_Origem

If by chance it is not possible to use CTE, you can try with a derived table:

select sum(soma) as soma
from
(
    -- consulta inicial
) as t

Edit: complete example with a derived table:

select sum(t.valor_t) as soma
from
(
  SELECT sc.empresa, sc.cod_autorizante, sc.codigo_ordem_compra, ( 
    SELECT SUM(ioc.valor_total) as soma 
    FROM itens_ordens_compras ioc 
    WHERE ioc.codigo_empresa = sc.empresa 
    and ioc.cod_ordem_compra = sc.codigo_ordem_compra) as valor_t 
  from solicitacao_compras sc 
  where sc.cod_autorizante = 222 
  and sc.data_aprovacao like '%/10/2020' 
  GROUP by sc.codigo_ordem_compra
) as t

I hope it helps

  • Thanks for answering imex, but I tried to use the first option and did not recognize the with. Already the second option yours, is the option I am using in select up there, it even adds the values, but I needed to add what is already being added to give a final total value of the records.

  • You must be using an old version that does not support CTE. On the second option, it is different from what you used, which in this case is a sub-base. I edited the post to add a full example, try some tests. I hope it helps

  • Oh okay, now I meant, without the example you put, I was thinking it was the same thing I’d already done, but now I see it’s not, you put one more select and inside it what I had done, perfect Imex, brought me the right result, that’s what I needed, thank you very much.

Browser other questions tagged

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