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.
Items of Purchase orders: 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:
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.
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.
– Bruno Henrique
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
– imex
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.
– Bruno Henrique