I have three tables customers, orders, and payments. payments table has a foreign key order_id, which references the orders table on the id field, however, the payments table can also accept payment without any order, i.e.( advance payment received without any order being placed.) conversely it is also possible that there could be orders without any payment e.g. orders created on credit without any payment.
now my query is I want to generate a day-closing table where I want to list all orders if any or all payments if any for a particular date.
I have tried to join the orders table with the payment table on order_date and payment_date but the result is not as per my requirement. i.e. if there are no orders on a particular date but there are payments then I don't get any results.
I have created a scenario on dB fiddle for the same. dbfiddle
if anybody can give me a solution, it will be an immense help.
as per the scenario on dB fiddle I tried the following query.
select
o.id,
o.order_date,
o.customer,
o.inv_number,
p.pymt,
p.cash,
p.bank
from
orders o
left join
(select
pymt_date,
sum(amount) pymt,
sum(cash) cash,
sum(bank) bank
from
payments
group by
pymt_date) p
on
o.order_date = p.pymt_date
where
o.order_date = '2022/10/25'
but I am not getting any results.