2
My need is to merge the result of two select commands so that I get everything back at once, for example in this command:
SELECT codigosuprimento, suprimento, count (codigosuprimento) quantidade, estoqueminimo,
count (codigosuprimento) - estoqueminimo as saldo
FROM public.estoque, public.suprimento where usado = '0' and codigosuprimento = 155 and
public.estoque.codigosuprimento = public.suprimento.codigo group by codigosuprimento, suprimento, estoqueminimo order by suprimento;
I bring the current situation of the code supply 155
in stock, My return is like this:
And in that command :
SELECT public.estoque.codigosuprimento,COUNT (public.estoque.codigosuprimento) as qtdSuprimento
FROM public.entrega, public.entregaitem, public.estoque
WHERE public.entrega.codigo = public.entregaitem.codigoentrega and codigosuprimento = 155 and
public.entregaitem.codigoestoque = public.estoque.codigo and public.entrega.data >= CURRENT_DATE - 30
GROUP BY public.estoque.codigosuprimento
ORDER BY codigosuprimento
I bring in how many supplies have been requisitioned in the last thirty days, and my return is like this:
My need would be to unite the results so that there would be something similar to this:
codigosuprimento | suprimento | quantidade | estoqueminimo | saldo | qtdSolicitada
155 | 50F0Z00 | 54 | 10 | 44 | 19
I did a test and I put one query
thus:
SELECT codigosuprimento, suprimento, count (codigosuprimento) quantidade, estoqueminimo,
count (codigosuprimento) - estoqueminimo as saldo,
(SELECT COUNT (public.estoque.codigosuprimento) as qtdSuprimento
FROM public.entrega, public.entregaitem, public.estoque
WHERE public.entrega.codigo = public.entregaitem.codigoentrega and codigosuprimento = 155 and
public.entregaitem.codigoestoque = public.estoque.codigo and public.entrega.data >= CURRENT_DATE - 30) as trinta
FROM public.estoque, public.suprimento where usado = '0' and codigosuprimento = 155 and
public.estoque.codigosuprimento = public.suprimento.codigo group by codigosuprimento, suprimento, estoqueminimo order by suprimento;
And so it brought me the expected result, but when I went to test to bring without the condition of bringing only to the code supply 155
it has already brought me meaningless results. I tried in this way to query
and brought me the wrong results:
SELECT codigosuprimento, suprimento, count (codigosuprimento) quantidade, estoqueminimo,
count (codigosuprimento) - estoqueminimo as saldo,
(SELECT COUNT (public.estoque.codigosuprimento) as qtdSuprimento
FROM public.entrega, public.entregaitem, public.estoque
WHERE public.entrega.codigo = public.entregaitem.codigoentrega and
public.entregaitem.codigoestoque = public.estoque.codigo and public.entrega.data >= CURRENT_DATE - 30) as trinta
FROM public.estoque, public.suprimento where usado = '0' and
public.estoque.codigosuprimento = public.suprimento.codigo group by codigosuprimento, suprimento, estoqueminimo order by suprimento;
Ever tried to give a
(select1...) UNION (select2...)
?– Zulian
@Zulian already, but to use the
UNION
both returns must have the same number of columns– R.Santos
Even putting the "additional" columns as
null
?– Zulian
The problem is that it brought the content of the second select in the second row and I need it to be in the same
– R.Santos
A subselect becomes but costly for db but I think it would suit you
– Caique Romero
Just like I put in my question @Caiqueromero?
– R.Santos
@Caiqueromero you would have some example of how to do with subselects?
– R.Santos
Instead of you fill 155 you column the code column of the first select
– Caique Romero
@Caiqueromero, how so?
– R.Santos
I demonstrated in the answer, see if I could help you
– Caique Romero