Question :
When this SQL query is executed it gives incorrect value, please advise
select
sum(cash_management.cash_in),
sum(cash_management.cash_out),
sum(cash_management.pay_in),
sum(cash_management.pay_out),
sum(cash_management.cash_declared),
sum(cash_management.cashin_drawer),
sum(order_master.order_quantity*order_master.order_item_price),
cash_management.date,
order_master.cashier_name,
cash_management.cashier_name
from cash_management inner join order_master
where cash_management.cashier_name=order_master.cashier_name
and date(date)='2015-02-19'
and date(order_time)='2015-02-19'
Answer :
Without knowing what data you are starting with in each table, what is being produced and what is wrong/what you expected – I can only make some general observations to help you:
1)Code Formatting – Format your code better, use table aliases to shorten your code or at least tidy it up a bit. You will find it easier to read and therefore debug.
2)At a guess, could it be your WHERE caluse at fault? If this is a sales ledger, are “date” and “order_time” always going to be the same date? If a record is inserted into a table and then a field is updated, you may find that the double WHERE is excluding records that you want included.
3)Since you have a join going on – do the required records (an order master record and a corresponding cash management record) exist on both tables?
As I said, no data and no examples so I can’t help – but these are my starters for 10.