using exists() to get the second row of the duplicate where id=id-1 and operatorid and location are the same:
select * from t where exists ( select 1 from t i where i.OperatorId = t.OperatorId and i.id = t.id-1 and i.Location = t.Location )
returns:
+-----+---------------------+----------+------+--------+------------+-------------+-------------------+ | id | datetime | location | site | client | operatorId | containerId | WorkflowId | +-----+---------------------+----------+------+--------+------------+-------------+-------------------+ | 102 | 2017-02-03 11:10:21 | PO | AA | TEMP | Test1 | 451 | 123F-258G-369147C | +-----+---------------------+----------+------+--------+------------+-------------+-------------------+
If
Id is not sequential, you could use a
common table expression with
row_number():
with t as ( select * , rn = row_number() over (order by id) from #temp ) select * from t where exists ( select 1 from t i where i.OperatorId = t.OperatorId and i.rn = t.rn-1 and i.Location = t.Location )