I have a problem with SQL Server. I have a table with machine statements, and another one with production data. The time between both tables are not the same to get the corresponding data. I don't know how to Join these tables. Also, I need to filter just the code 17 from Table A.
For example:
Table a:
Machine Code Time
----------------------------------
EPSX 17 2018-08-28 12:09:32
EPSA 19 2018-08-28 12:09:39
EPSC 17 2018-08-28 11:10:58
Table b:
Machine Waste(meters) Time
-------------------------------------------
EPSX 57m 2018-08-28 12:09:38
EPSA 128m 2018-08-28 12:09:43
EPSC 12m 2018-08-28 11:11:02
Desired output:
Machine Code Waste(meters)
----------------------------
EPSX 17 57m
EPSC 17 12m
I'm thinking to get the relation with the following statement:
a.Machine = b.machine (this is easy)
AND a.Time > b.Time
AND a.Time <= DATEADD(MINUTE, 2, b.Time)
How can I solve this? Maybe using function? I can get the correct directly from the function from table b. But can I call the Table a and use a.Time in function call?