I have problem to select direct siblings of the main row. For example this is my data I need to filter:
+------+------------+------------+
| id | from | to |
+------+------------+------------+
| 2265 | 2016-03-30 | 2016-04-09 |
| 1420 | 2016-03-30 | 2016-04-11 |
| 2261 | 2016-03-30 | 2016-04-12 |
| 2262 | 2016-04-01 | 2016-04-12 |
| 1296 | 2016-04-01 | 2016-04-24 |
| 1053 | 2016-04-01 | 2016-05-01 |
| 2302 | 2016-04-30 | 2016-05-24 |
| 2025 | 2016-05-14 | 2016-06-06 |
| 2392 | 2016-05-20 | 2016-05-28 |
| 2033 | 2016-05-21 | 2016-06-08 |
| 2389 | 2016-05-25 | 2016-06-09 |
+------+------------+------------+
All I need is to select row and it's direct siblings by min/max dates. For example, result should be:
+------+------------+------------+
| id | from | to |
+------+------------+------------+
| 1296 | 2016-04-01 | 2016-04-24 |
| 1053 | 2016-04-01 | 2016-05-01 |
| 2302 | 2016-04-30 | 2016-05-24 |
+------+------------+------------+
Where row 1053
is the "main" row.
I know it's something with MIN
and MAX
functions, but I can't figure how to use it. I could do it in my app, but I'd rather do it in SQL because of performance.