I am trying to write a Microsoft SQL Server query for retrieving the oldest record in which the text fields are the same, but the dates are 30 seconds or less apart. Here is an example:
My table:
RecordID TextField1 TextField2 DateField1
--------------------------------------------------------------------------------
1 SomeData1 SomeData2 9/11/2011 2:33:00pm
2 SomeData3 SomeData4 9/11/2011 2:33:15pm
3 SomeData3 SomeData4 9/11/2011 2:33:18pm
4 SomeData3 SomeData4 9/11/2011 2:42:12pm
5 SomeData1 SomeData2 9/11/2011 2:33:01pm
6 SomeData6 SomeData7 9/11/2011 2:33:01pm
7 SomeData1 SomeData2 9/12/2011 2:33:00pm
8 SomeData6 SomeData8 9/11/2011 2:33:03pm
Okay, so in this example, I want a query that will pull the rows in which TextField1=TextField1 and TextField2=TextField2 and the dates between them are 30 seconds or less (I want the oldest of the two returned). So the query, in this example, should return:
RecordID TextField1 TextField2 DateField1
--------------------------------------------------------------------------------
1 SomeData1 SomeData2 9/11/2011 2:33:00pm
2 SomeData3 SomeData4 9/11/2011 2:33:15pm
RecordID 8 is not returned because TextField2 is different.
Hopefully I explained this clearly enough. Any help would be appreciated!
I couldn’t understand everything on your question.
This is a generic SQL query that will compare the records of you table, against the same table, looking for records with different RecordID, but equal TextField1 and TextField2.
Leave a comment if this looks like what you want and we can improve this query to get exactly what you are looking for.
UPDATED: