I have 5 tables and trying to Join into 1 but I am getting 4 records for each. How can i use correct JOIN statement.
SELECT tbl_meter.[cdate] AS 'CDate',
tbl_meter_backup.[machine_no],
tbl_machines.[accounting_denomination] AS 'Denom',
(tbl_meter.[turnover]- tbl_meter_backup.[turnover])* tbl_machines.[accounting_denomination] AS 'Turnover',
(tbl_meter.[total win]- tbl_meter_backup.[total win])* tbl_machines.[accounting_denomination] AS 'Total win',
(tbl_meter.[games played]-tbl_meter_backup.[games played]) AS 'Games Played', (tbl_meter.[credit in]-tbl_meter_backup.[credit in]) * tbl_machines.[accounting_denomination] AS 'Credit IN',
(tbl_meter.[Bill in]-tbl_meter_backup.[Bill in])* tbl_machines.[accounting_denomination] AS 'Bill In',
(tbl_meter.[cancel credit]-tbl_meter_backup.[cancel credit])* tbl_machines.[accounting_denomination] AS 'Cancel Credit',
tbl_open_backup.[amount] AS 'cgOpen',
tbl_cancel_backup.[amount] AS 'cgCancel'
FROM
tbl_machines,
tbl_meter,
tbl_meter_backup,
tbl_open_backup,
tbl_cancel_backup
INNER JOIN tbl_meter ON
tbl_meter_backup.[Machine_No] = tbl_meter.[Machine_No] AND tbl_machines.[local_no]=tbl_meter.[machine_no] AND tbl_open_backup.[machine_no]=tbl_meter.[machine_no] AND tbl_cancel_backup.[machine_no]=tbl_meter.[machine_no]
WHERE tbl_meter_backup.[cDate] = @StartDate AND tbl_meter.[cDate] = @EndDate AND tbl_open_backup.[cdate]=@enddate AND tbl_cancel_backup.[cdate]=@enddate;
First fix your synatx to explicit joins that are easier to read and maintain, there is no excuse for ever using an implicit join, they are error prone and 18 years outdated and especially hard to maintain when you mix them like this. I expect that the system could have been confused on this as well as you didn’t do the joins correctly. In an implicit join, the join criteria goes in the where clause not the on clause of the one explicit join you used. You also joined to the same table twice.
So with the cleaned up joins do you still have a data issue?
If so, one or more of your tables has more that one record for the items in the join. This will then need special handling as you need to determine how to know which record you want.