Below I make an explanation of what I do and what I need, now I leave an approach of the target table and the query that I use.
Target table (fact_accounting_lines):
id_producto | unds | compra_venta 10517931 | 2 | Compra 10517929 | 4 | Venta 10517929 | 2 | Venta 10517939 | 6 | Compra 10517931 | 3 | Venta
This is my query:
SELECT
fact_contabilidad_lineas.id_producto,
SUM(fact_contabilidad_lineas.unds) as unds,
fact_contabilidad.compra_venta
FROM fact_contabilidad, fact_contabilidad_lineas
WHERE fact_contabilidad_lineas.serie = concat_ws('-', fact_contabilidad.id_serie, fact_contabilidad.serie_num)
AND fact_contabilidad_lineas.iduser = 98
AND fact_contabilidad.iduser = 98
AND fact_contabilidad.compra_venta = 'Venta'
AND fact_contabilidad_lineas.id_producto > 10517927
GROUP BY fact_contabilidad_lineas.id_producto
Result:
id_producto | unds | compra_venta 10517929 | 6 | Venta 10517931 | 3 | Venta
I have not shown the table (fact_accounting) because it is not necessary, it does not influence my question and in this way the question is somewhat cleaner. The query works fine but it only gets me the lines that are buy_sell = Sell.
What I need to get is:
1 column the sum of the products grouped by product_id and buy_sell = Sale .
1 column the sum of the products grouped by product_id and buy_sell = Buy .
Expected result:
id_producto | Venta | Compra 10517929 | 6 | 6 10517931 | 3 | 2
Can you guide me? I can't think of how to do it.
You are looking for something like this:
First of all, please, modify your question and pass us the data in SQL, this way it will be much easier to reproduce your problem:
Likewise, if you see that the table
fact_contabilidad
does not contribute much, remove it from your query. So we will not have to modify it to do the tests:Once at this point, we can help you. By the way, very good that you have put the expected result. Thus we have been able to see that one of the data that you pass us had an error: the fourth record ends in 39 instead of 29, remember to modify it in your dataset to obtain the same results.
And now, finally, the explanation of the SQL: you really almost had it, you had made the groups with
GROUP_BY
and the totals withSUM
; the only flaw had been to make a selection only for sales and leave out purchases. Eliminating that condition fromWHERE
you could have already used the functionIF
to add only what you were interested in in each column.In any case, I don't particularly like to use
IF
it because, in a way, you're forcing yourself to read all the records in each group. I prefer something like this:It seems longer, but in cases where it
compra_venta
had more than two values, it would be more efficient. Also, it has relational algebra representation, while heIF
is more programmer style...If there is something that you do not see clearly, please, leave me your doubt in the comments and I will expand the answer as necessary, because each of these functions has a lot of crumb...
As you put that result there, you could get it with this query I think: