Complex Query SQL


Is there a merciful soul who can help me with complex sql queries on CakePHP 4.x?

"SELECT SUM(a.Qte_In - b.qte_delivred) AS Stock
FROM	(SELECT product_id, SUM(Qte_In) AS Qte_IN FROM besdtls WHERE product_id = 1 GROUP BY product_id) As a
JOIN	(SELECT product_id, SUM(Qte_Delivred) AS Qte_Delivred FROM orderdetails WHERE product_id = 1 GROUP BY product_id) As b ON b.product_id = a.product_id
WHERE	a.product_id = 1";


What exactly is it that you’re struggling with? What have you tried so far?

I don’t know how to do it that’s all

I just need to calculate the current stock
I need to take the sum of qte_in from the besdtls table
and i have to take sum of qte_delivered from orderdetails table

You don’t know how to do what? Executing raw SQL? Using the query builder? Figuring the correct SQL for whatever you’re trying to do? …?

that’s what i did in the end