I have 3 SQLite tables customer: sale and invoice. The sum that I get on invoice.amount
is incorrect in the below query but the sum.amount
on sale table is correct.
My query is:
SELECT sum(invoice.amount) as 'amt'
, customer.name
, sum(sale.amount) as 'amt1'
FROM customer
INNER JOIN sale on customer.customer_id = sale.customer_id
INNER JOIN invoice on customer.customer_id = invoice.customer_id
WHERE (
(sale.date <='2022-04-30') and
(invoice.date <='2022-04-30') and
customer.area='xyz' and
(
customer.status='Active' OR
customer.status='Inactive'
)
)
GROUP BY customer.customer_id
ORDER BY customer.delseq ASC
If I only use one inner join as shown below, and skip sale table, then I get correct results.
SELECT sum(invoice.amount) as 'amt'
, customer.name
FROM customer
INNER JOIN invoice ON customer.customer_id = invoice.customer_id
WHERE (
( invoice.date <='2022-04-30') and
customer.area='xyz' and
(
customer.status='Active'
OR
customer.status='Inactive'
)
)
GROUP BY customer.customer_id
ORDER BY customer.delseq ASC