tags:

views:

44

answers:

1

Hello to everyone,

I have 2 different table which is tbl_meter and tbl_machines. I am entering data using tbl_meter every day. I am finding dailiy results with following sql syntax. But I have problem with tbl_machines. There is relation between 2 tables with local_no and machine_no fields. How can I fetch fileds from tbl_machines table and JOIN to tbl_meter table. I have already JOIN statement in first table. If anyone can help me I would be really appraciated. This is sql syntax that I am calculating results from tbl_meter. I would like to JOIN 2 fields from tbl_machines to tbl_meter.

SELECT ((B.[turnover]*1) - (A.[turnover]*1)*1-((B.[total win]*1)*1 - (A.[total win]*1)*1)) As 'Result',
       A.[Machine_No] As 'Machine_No', (B.[turnover]*1) - (A.[turnover]*1) As 'Turnover', (B.[total win]*1) - (A.[total win]*1) As 'Total win',
       (B.[games played]*1) - (A.[games played]*1) As 'games played', 
       (B.[Credit In]*1) - (A.[Credit In]*1) As 'Credit In', 
       (B.[Bill In]*1) - (A.[Bill In]*1) As 'Bill In', 
       (B.[Cancel credit]*1) - (A.[Cancel credit]*1) As 'Cancel credit', 
       ((((B.[total win]*1)*1 - (A.[total win]*1)*1))*1 / ((B.[turnover]*1) - (A.[turnover]*1)*1))*1 As 'Actual%' 
  FROM tbl_meter A 
INNER JOIN tbl_meter B ON A.[Machine_No] = B.[Machine_No] 
     WHERE A.[cDate] = @StartDate
       AND B.[cDate] = @EndDate;
A: 

You can join on multiple fields, e.g.

INNER JOIN tbl_machines MAC
ON MAC.local_no = A.local_no
AND MAC.machine_no = A.machine_no
Will A
Ok but I have already Join statement on above code. How can I do that with second JOIN statement? Sorry for stupid questions but I am beginner.
Hakan
You can have as many joins as you need in a single query - A INNER JOIN B ON A.f = B.f INNER JOIN C ON C.g = B.g INNER JOIN D ON D.h = C.h etc.
Will A
@Hakan: If I understand your requirement correctly, you need to **replace** the part of your query that reads INNER JOIN tbl_meter B ON A.[Machine_No] = B.[Machine_No] with Will A's sample code - you will also need to change MAC to B in his code.
Mark Bannister