1
I have 2 selects below, how can I join in only 1 select?
I need to take everything that sold in a certain period (BETWEEN) by item only that at the same time I have to take what had stock of the item up to the initial date.
to separate whether it is input or output is d.type option
Example:
Produto Estoque qtd vendida
XPTO 100 50
Select 1:
SELECT DISTINCT d.cat_id prod, p.id, p.nome id_prod,
SUM( d.qtd ) AS qtd_prod, SUM( d.valor_total ) AS total_prod
FROM lc_controle c
INNER JOIN lc_detalhe d ON d.controle_id = c.id
INNER JOIN `lc_cat` p ON p.id = d.cat_id
WHERE d.tipo = '0'
and c.dtConcat BETWEEN '$dtinicial' AND '$dtfinal'
and c.exportado = 'Sim'
GROUP BY d.cat_id
ORDER BY qtd_prod DESC
Select 2:
SELECT DISTINCT d.cat_id prod, p.id, p.nome id_prod,
SUM( d.qtd ) AS qtd_estoque
FROM lc_controle c
INNER JOIN lc_detalhe d ON d.controle_id = c.id
INNER JOIN `lc_cat` p ON p.id = d.cat_id
WHERE d.tipo = '0'
and c.dtConcat < '$dtinicial'
and c.exportado = 'Sim'
GROUP BY d.cat_id
ORDER BY qtd_estoque DESC
John, thanks for the help...but the second part he is not adding up the quantities and is bringing all the items and not just the items that have movement of the first part......
– Robert
The SUM is not being done in the 2nd SELECT? Strange... if you run each query individually, what results have?
– João Martins
If I turn only the first part the result is the same with the second part, the problem is in the second part, I manually added the Qtd of the first part and is bringing the correct quantities...I have an item that it shows in the first part and I had output in the reported period of 426 units and input until the initial date that is in the second part of 600 units and it shows zero
– Robert
Attention that in the second consultation he will add only quantities prior to the initial date! I do not know if you are taking this into account.
– João Martins
yes that’s just what I need, that item I told you his input was before the initial date so it should have added
– Robert
Are you sure that the SELECT who put in his question return data? Using the INNER JOIN the table lc_control must have records in the lc_detail and this in lc_cat. Confirm pf the information
– João Martins
Yes return the information only that separate, had a query for entries and other for exits, I need to unify precisely to have the 2 information in the same report, so: the user chooses the initial date and final date for consultation the report brings all the items that had movement in this period as follows: Name of the product quantity sold input quantity and total value of sale
– Robert
Let’s go continue this discussion in chat.
– João Martins