Hi Ruben12345,
Refer the below sample query and change the where condition as per your need.
DECLARE @A AS TABLE(id INT,shipment INT,vehicle_name VARCHAR(20),vehicle_id INT,[begin] DATETIME)
INSERT INTO @A VALUES(123,11111,'B9373VO',10884919,'2017-05-19 11:00')
DECLARE @B AS TABLE(vehicle_id INT,[date] DATETIME,[type] INT,[description] VARCHAR(20),geofence VARCHAR(10))
INSERT INTO @B VALUES(10884919,'2017-05-19 11:30:00.000',31,'EXIT','Aps')
INSERT INTO @B VALUES(10884919,'2017-05-19 12:00:00.000',30,'ENTER','Uli')
INSERT INTO @B VALUES(10884919,'2017-05-19 11:00:00.000',13,'STOP','IDLING')
SELECT A.id,A.shipment,A.vehicle_name,A.vehicle_id,A.[begin]
,B.[date],B.[type],B.[description],B.geofence
FROM @A A
INNER JOIN @B B ON A.vehicle_id = B.vehicle_id
WHERE B.[type] IN (31,30)
OutPut
id |
shipment |
vehicle_name |
vehicle_id |
begin |
date |
type |
description |
geofence |
123 |
11111 |
B9373VO |
10884919 |
2017-05-19 11:00:00.000 |
2017-05-19 11:30:00.000 |
31 |
EXIT |
Aps |
123 |
11111 |
B9373VO |
10884919 |
2017-05-19 11:00:00.000 |
2017-05-19 12:00:00.000 |
30 |
ENTER |
Uli |