Hi All,
I need some basic help with a SQL query to join 3 tables together. I have three tables that have a common field across them and when joining two together I get the expected results, however when trying to join the 3 tables the query times out.
Table A has the following fields
- ID
- UserID
- Name
- Branch
Table B has the following fields
- ID
- UserID
- Date
- Workday
- In
- Out
Table C has the following fields
- ID
- UserID
- Checktime
Table C has multiple entries for each UserID, when a user signs in at our wall clock an entry is put created here, this can multiple times a day (simply by the user double scanning or not sure if they did so they scan again - however we are only taking the first entry for IN and the last entry as OUT.
From here the data is moved into Table B and each entry is put into the columns, The issue we have is that Table B isn't getting filled out properly even though there is a valid record in table C.
I want to join the three tables together to run a little report to show me the following output
A.Name, A.Branch, B.Date, B.In, B.Out, C.Checktime
I'm hoping it will provide me an easy report to identify the records that aren't coming across from table C to table B.
The query I have thus far is:
select A.Name, A.Branch, B.Date, B.In, B.Out, C.Checktime from Table A
JOIN Table B ON A.UserID = B.UserID
LEFT JOIN Table C ON U.UserID = W.UserID
where A.DayType = 'Workday' and A.Date >= '2018-06-01'
So I'm expecting something like
John Smith, Head Office, 2018-06-01, NULL, NULL, 2018-06-01 10:00
Foo Bar, Singapore, 2018-06-01, 2018-06-01 8:27, NULL, 2018-06-01 8:27
Foo Bar, Singapore, 2018-06-01, 2018-06-01 8:27, NULL, 2018-06-01 17:00
From here, I can tell that John Smith had signed in but the record didn't get brought across to table B, but I can see that Foo Bar signed in and the record from table C flowed through table B for the sign IN but Foo Bar sign out at 5pm didn't flow through to table B.
I appreciate your time and help people.
Thanks Jason