I'm looking for a way to filter out the data within a range in the MS SQL query. In this case "Test2" is my starting point and "Test4" is my ending point. I want to filter out "Test1" as long it is between "Test2" and "Test4" rows. If it's outside then I want to keep it. And also I want to keep "Test3" row.
Actual Result:
| DateTime | Message |
|---|---|
| 2022-09-09 00:00 | Test1 |
| 2022-09-09 01:00 | Test2 |
| 2022-09-09 02:00 | Test1 |
| 2022-09-09 03:00 | Test1 |
| 2022-09-09 04:00 | Test3 |
| 2022-09-09 05:00 | Test4 |
Expected Result:
| DateTime | Message |
|---|---|
| 2022-09-09 00:00 | Test1 |
| 2022-09-09 01:00 | Test2 |
| 2022-09-09 04:00 | Test3 |
| 2022-09-09 05:00 | Test4 |
2nd example
Actual Result:
| DateTime | Message |
|---|---|
| 2022-09-08 11:00 | Test3 |
| 2022-09-08 12:00 | Test1 |
| 2022-09-09 01:00 | Test2 |
| 2022-09-09 02:00 | Test1 |
| 2022-09-09 03:00 | Test1 |
| 2022-09-09 04:00 | Test3 |
| 2022-09-09 05:00 | Test4 |
Expected Result:
| DateTime | Message |
|---|---|
| 2022-09-08 11:00 | Test3 |
| 2022-09-08 12:00 | Test1 |
| 2022-09-09 01:00 | Test2 |
| 2022-09-09 04:00 | Test3 |
| 2022-09-09 05:00 | Test4 |
3rd example
Actual Result:
| DateTime | Message |
|---|---|
| 2022-09-08 11:00 | Test3 |
| 2022-09-08 12:00 | Test1 |
| 2022-09-09 01:00 | Test2 |
| 2022-09-09 02:00 | Test1 |
| 2022-09-09 03:00 | Test1 |
| 2022-09-09 04:00 | Test3 |
| 2022-09-09 05:00 | Test4 |
| 2022-09-09 06:00 | Test1 |
Expected Result:
| DateTime | Message |
|---|---|
| 2022-09-08 11:00 | Test3 |
| 2022-09-08 12:00 | Test1 |
| 2022-09-09 01:00 | Test2 |
| 2022-09-09 04:00 | Test3 |
| 2022-09-09 05:00 | Test4 |
| 2022-09-09 06:00 | Test1 |
Any help wil be appreciated.
SOLVED:
I managed to solve the problem thanks to the query provided by @Ahmed. Thanks to everyone that took the time to solve this.
Thank you!
