Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Find double records within DateTime range

$
0
0
Hi, thanks for reading my topic.I have a query question.Consider a table with the following structure:RecordID (PK - int) - RecordDate (DateTime)I need to find all records that fall within a 7 day period slot based on the first RecordDate of a specific slot.Example, consider the following records:RecordID - RecordDate1 - 2015-04-01 14:002 - 2015-04-03 15:003 - 2015-04-03 16:054 - 2015-04-03 19:235 - 2015-04-06 09:156 - 2015-04-06 11:307 - 2015-04-07 12:008 - 2015-04-09 15:15The result of the query I'd like should look something like this12578So basically I'd like to leave record 3 and 4 out because they fall within 24 hours of record 2 and I'd like to leave record 6 out because it falls within 24 hours of record 5.I'd tried working with a CTE and set a dateadd(d, 1, recorddate), join it on itself and use a between From / To filter on the join but that didn't work. I don't think NTILE will work with this?I'd love to hear some suggestion on how to approach this problem.Thanks and have a great day.

Viewing all articles
Browse latest Browse all 3145

Trending Articles