SUM(quantity) AS sale
and
SUM(Rquantity) AS order_return
— is not effect (change) according to declared date . please help me
SELECT I.target_id as Iid,
I.target_date as Idate,
I.customer_name as Icust,
I.target_product as Icat,
I.target_pay as Ipay,
I.target_rec as Irec,
I.active as Iact,
I.status as Ista,
COALESCE(S.sale, 0) AS sale,
COALESCE(P.cat_n, 0) AS cat_n,
COALESCE(R.cus_n, 0) AS cus_n,
COALESCE(O.order_return, 0) AS order_return,
I.target_pay - (S.sale - O.order_return) AS T_achi
FROM target_value I
LEFT JOIN (
SELECT categories_id, categories_name AS cat_n
FROM categories
) P ON P.categories_id = I.target_product
LEFT JOIN (
SELECT customer_id, customer_name AS cus_n
FROM customer
) R ON R.customer_id = I.customer_name
LEFT JOIN (
SELECT order_id, order_date, categories_id, SUM(quantity) AS sale
FROM order_item
GROUP BY categories_id
) S ON S.categories_id = I.target_product
LEFT JOIN (
SELECT order_id, order_date, categories_id, SUM(Rquantity) AS order_return
FROM order_item
GROUP BY categories_id
) O ON O.categories_id = I.target_product WHERE S.order_date BETWEEN '2024-09-01' AND '2024-09-24';
Summation not work with declared date . please help me
3